Good day! I have a static listOfStudents() method that opens a text file and reads my data from there ...
My text file looks like this:
As you can see in the picture a simple text file with data.
My task is to take from there the Name and Surname and display:
Fully my method looks like this:
static void listStudents(){ cout << endl << "List students" << endl<< endl; ifstream textfile; string line; string unused = ""; textfile.open("students.txt"); if(textfile.is_open()){ int count = 0; for(int i = 0 , j = 0; !textfile.eof(); i++){ if(i == j){ count++; textfile >> line; cout << count << ": " << line; textfile >> line; cout << " " << line << endl; j+=11; }else{ textfile >> unused; unused = ""; } } }else{ cout << "You don't have any students!" << endl; } textfile.close(); cout << endl << endl; main(); } I have 4 students in a text file, and when I display the following happens:
As you can see, everything shows up normally, but at the end, for some reason, it generates another 1 line where the name of the previous student is doubled.
Question: Why does he do that? And how can I fix this?


!eofis almost always an error in C and C ++. - AnT