linked list problem

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

When I open up my 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


#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
All you are doing is reading the pid field and nothing else. The second
time you attempt to read the file (ie, the first time through your while loop)
you are attempting to read the word "jack" into an int. Since "jack" is not
a valid integer, this causes the input stream to go into an error state.

You need to read all of the fields on the first line before you can read
the pid of the second item.
Topic archived. No new replies allowed.