# C++ Notes: End-Of-File (EOF)

## End-Of-File (EOF) when reading from cin

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.

Dev-C++
  Type: Enter Control-z Enter
MS Visual C++
  Type: Enter Control-z Enter Enter
  Reportedly there is a Microsoft patch that can be applied so that only one Enter is required after the Control-z. I wouldn't bother.
Other systems
  Some may use other characters: control-D then Enter, or control-D followed by a control-Z, or ... .

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!

## Resetting after EOF

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;
    ...