end of file controlled while loop not working as required

Mike 85 83 77 91 76 CSC
Mark 80 90 95 93 48 CSC
Anderson 78 81 11 90 73 BUSS
Blair 92 83 30 69 87 ECE
Suzy 23 83 30 69 87 ARC
Carlos 46 76 90 54 38 MASS-COMM

So i have this file and i am required to output the Name,Scores & Faculty using a struct. This is where i am stuck, My following code gives only Carlos's Data and ignores all above him. what should i change to fix it and why ?


struct student {

string name;
int scores[5];
string faculty;

};


int main() {

student x;


ifstream myfile("D:\\Test\\MIUCS.txt");

while (!myfile.eof()) {

myfile >> x.name;


for (int j = 0; j < 5; j++) {

myfile >> x.scores[j];
}


myfile >> x.faculty;

}

cout << x.name << " ";

for (int k = 0; k < 5; k++) {

cout << x.scores[k] << " ";

}


cout << x.faculty << endl;


}
Hello Meso,

Welcome to the forum.

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

I see two problems:

1. Is the while condition based on "eof".

while(!inFile.eof())

I do not know why people teach this. This is a bad idea as it does not work the way you think it will. Generally by the time the while condition determines that "eof" has been reached you will have processed the last read twice and wondering why it shows up twice.

What happens is the while condition is checked and “eof” has not been set yet. After entering the while loop you read the last line or bit of information from the file process the information reach the bottom of the while loop. At this point “eof” has not been set yet and you return to the while condition which is still good. After entering the loop you try to read from the file, but now there is nothing left to read and “eof” is set, but you do nothing at this point to deal with the “eof” condition. So you process what information is in the variables from the last read before returning to the while condition where it finally detects the “eof”, but you have processed the last good read twice unless you have cleared the variables at the end of the while loop in which case the last process will be blank.
A more acceptable way of using the while loop is:

1
2
3
4
5
while (infile >> someVariable)
{
    infile >> additionalVariables;
    //  Additional code.
}


In the above example you can set up the while condition to read one variable, two or more variables or use “std::getline(?, str)” where ? could be from “std::cin” or from a file stream and “str” represents any “std::string” variable.

2. You are print the output after the while loop, so you are only getting the last read from the file to use.

You could either put the output inside the while loop or in the while loop store the information in the struct in an array or as I would use a vector. Then step through the vector to print the information from each element. This way you would get every line.

Hope that helps,

Andy
Topic archived. No new replies allowed.