issues counting names from text file


1
2
3
4
5
6
7
8
9
10
11
12
  while (!names.eof())
    {
      names >> thenames;
      if (!names.eof()) break;
      pplcounter++;

    }
}
else {
    names.close();
    cout << "Error" << endl;
    return 0;



However the output will = 0. When in fact it is suppose to be a counter of lines used if I understand correctly. The txt file could be.

Bob
Dylan
Jessica
Jerry

Therefore, it should output a 4, no? My guess it stops at the break statement maybe I'm overlooking something I dunno.
Last edited on
If not end of the file you exit the loop. So the function returns 0.
Last edited on
Look at this if (!names.eof()) break;

In the first iteration of the while-loop, it hits this if-statement and it's NOT at the end-of-file. That condition evaluates to true, so break is taken. Since it hasn't reached the pplcounter++ yet, it breaks out of the loop before incrementing your counter variable??
Last edited on
ah that makes so much sense that I can only laugh at myself thanks.
Topic archived. No new replies allowed.