I just found a comment in this answer saying that using iostream::eof
in a loop condition is "almost certainly wrong". I generally use something like while(cin>>n)
– which I guess implicitly checks for EOF.
Why is checking for eof explicitly using while (!cin.eof())
wrong?
How is it different from using scanf("...",...)!=EOF
in C (which I often use with no problems)?
Best Answer
Because
iostream::eof
will only returntrue
after reading the end of the stream. It does not indicate, that the next read will be the end of the stream.Consider this (and assume then next read will be at the end of the stream):
Against this:
And on your second question: Because
is the same as
and not the same as