Hi
I was wondering if someone could help with linked list. I'm trying to read i a text file in this format:

ID  Name        PR  Salary
339 GOERGE      4   26000
221 SANDY       4   22600   
101 RONNY       3   35250

and put it in a linked list. I have this much so far.

struct employee
{
    int ID;
    char name[10];
    int performance;
    int curSalary;
    employee *next;
};
int main()
{
    ifstream inFile;
    employee *head, *current = NULL;

    inFile.open("InputFile.txt");
    if (!inFile) 
    {
        cerr << "Unable to open file datafile.txt" << endl;
        exit(1);   // call system to stop
    }
    inFile.getline(inFile,80);


    while(!inFile.eof())
    {


    }
}

Edited 3 Years Ago by mike_2000_17: Fixed formatting

1) give some of your thoughts;
2) see the reading of the file and list working manuals

I think you have a decent start. There are links on this site that discuss some of the potential perils of using the loop control style that you used (using "eof") that you may want to look at.

while(!inFile.eof())
{
}

Here is one of them. There are others: http://www.daniweb.com/forums/post155265-18.html

I would consider, since you know the format of your data file, changing this:

inFile.getline(inFile,80);

to something that reads directly into your data members so you don't have to parse a string later (I don't think your getline command would work anyway since you have not provided a string where you want to store the data that you read in from the file). Something like this:

employee  anEmployee;

inFile >> anEmployee.ID;
inFile >> anEmployee.name;
inFile >> anEmployee.performance;
inFile >> anEmployee.curSalary;

anEmployee.next = NULL;
head = &anEmployee;
current = &anEmployee;

That primes the loop with the first employee. It'll be your head node. Progressing inside the loop (again, consider changing the "eof" control condition part of it) you can read in the data in a similar method and adjust the "next" member of the previous employee read in from the file to point to the one that was just read in.

This article has been dead for over six months. Start a new discussion instead.