[15.5] Why does my input seem to process past the end of file?
Because the eof state may not get set until after a read is attempted past the
end of file. That is, reading the last byte from a file might not set the eof
state. E.g., suppose the input stream is mapped to a keyboard — in that case
it's not even theoretically possible for the C++ library to predict whether or
not the character that the user just typed will be the last character.
For example, the following code might have an off-by-one error with the count
i:
int i = 0;
while (! std::cin.eof()) { // WRONG! (not reliable)
std::cin >> x;
++i;
// Work with x ...
}
What you really need is:
int i = 0;
while (std::cin >> x) { // RIGHT! (reliable)
++i;
// Work with x ...
}