Jul 10, 2013 at 9:18pm UTC
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.
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 Jul 11, 2013 at 9:36pm UTC
Jul 10, 2013 at 9:30pm UTC
If not end of the file you exit the loop. So the function returns 0.
Last edited on Jul 10, 2013 at 9:30pm UTC
Jul 10, 2013 at 9:32pm UTC
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 Jul 10, 2013 at 9:38pm UTC
Jul 10, 2013 at 9:37pm UTC
ah that makes so much sense that I can only laugh at myself thanks.