When I excute my program and open up the text file, it only reads me the 1234 then junk below it. I have no clue what do. Anything is welcome. Below is my .cpp file

my text file looks something like this

1234 jack hammer 75.00 .45 10
5678 Daffy Drill 25.00 .67 10
9887 Flash light 30.00 .87 10

#include<iostream>
#include<conio.h>
#include<fstream>
#include<iomanip>
using namespace std;

//******************************************************************************

struct acmetype{
int pid;
char description[25];
float wholesale;
float markup;
int quantity;
acmetype *link;
};

ifstream fin; //global variable

void headerfn();
void fileopen();
void inputfn(acmetype *&head);
void printfn(acmetype *&head);


//*******************
//* Begin of Main *
//*******************

int main(){
system("color F0");
headerfn();
fileopen();

acmetype *head;
inputfn(head);
printfn(head);



cout<<endl<<endl;
system("PAUSE");

}//end of main

//******************************************************************************
void fileopen(){
string tools;
int pid;
char description;
fin.open("tools.txt");

if (!fin)
{
cout << "Error opening output file. Go Play Basketball like Kobe!!"<<endl<<endl;
system("PAUSE");
getch();
}



}

//***************************************************************************
void inputfn(acmetype *&head){
acmetype *currptr, *nnptr;
int counter= 0;
char description[25];

head = new acmetype;//creating first node
fin>>head->pid;
currptr = head;//curr ptr pointing at head and first node is ready.
counter++;
while(fin){
nnptr= new acmetype;
fin>>nnptr->pid;
fin.getline(description, 25);
// fin.getline(acmetype.description[25]);

currptr->link = nnptr;
currptr = currptr->link;
if(fin.peek()=='\n')fin.ignore();//check for next character
counter++;
}//end of while
currptr->link= NULL;
currptr= NULL;
nnptr= NULL;

}//end of inputfn
//****************************************************************************
void printfn(acmetype *&head){
acmetype *currptr;

//print all data members
currptr = head;
while(currptr!=NULL){
cout<<currptr->pid<<endl;
currptr=currptr->link;
if(fin.peek()=='\n')fin.ignore();
}//end of while

line 77: >> fin.getline(description, 25);

Unfortunately that doesn't work because the description field is not a fixed size of 25 characters in the data file. What you need to do is just read the next two words and concantinate them together: something like this

std::string temp;
fin >> nnptr->description;
fin >> temp; temp = " " + temp;
strcat(nnptr->description, temp.c_str();

1234 jack hammer 75.00 .45 10
5678 Daffy Drill 25.00 .67 10
9887 Flash light 30.00 .87 10
4567 Carbide tip blade 13.95 .50 10
5678 sli welder 375.00 .25 5

would that make it description[25]? I really appreciate the feed back.

getline() reads 25 charcters, or up to end-of-line, which ever is shorter. In order for that to work the description field in the data file must always be 25 characters (fixed length). The newest file example you just posted has 3 words in some description fields, which tosses out my previous suggestion. Here is how I would do that NOTE: This has not been compiled or tested.

std::string line;
while( getline(fin, line) )
{
    size_t pos = 0;
     // find first non-digit character
    while( idsigit(line[pos]) )
          pos++;
    // extract pid
    nnptr->pid = atol( line.substr(0,pos).c_str());
    line = line.substr(pos+1);
    // find first digit field (end of description)
    for(pos = 0; !isdigit(line[pos]); pos++)
        ;
    // extract description
    strcpy(nnptr->description, line.substr[0,pos].c_str());
    line = line.substr(pos);
    // convert remainint integers
    strstring str(line);
    str >> nnptr->wholesale >> nnptr->markup >> nnptr->quantity;
    // add nnptr to linked list
    <snip>
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.