When a program is reading from a disk file, the system "knows" when it gets to the end. This condition is called End-Of-File (EOF). All systems also provide some way of indicating an EOF when reading from the keyboard. This varies from system to system.
You can just provide bad data to
make cin
fail in many cases. A student once claimed
that typing "EOF" was the way to indicate and end-of-file from
the console. Yes, it stops reading (because of an error) if you're reading numbers, but
not when reading characters or strings!
Altho it doesn't make sense to read after an EOF on a file,
it is reasonable to read again from the console after an
EOF has been entered. The clear
function allows this.
while (cin >> x) {
... // loop reading until EOF (or bad input)
}
cin.clear(); // allows more reading
cin >> n;
...