How to signify no more input for string ss in the loop while (cin >> ss)

As others already answer this question, I would like add this important point:

Since Ctrl-Z on Windows (and Ctrl-D on unix systems) causes EOF to reach, and you exit from the while loop, but outside the while loop you cannot read further input, since the EOF is already reached.

So to enable reading using cin again, you need to clear eof flag, and all other failure flags, as shown below:

cin.clear();

After doing this, you can start reading input using cin once again!


Your code is correct. If you were interactively inputting, you would need to send a EOF character, such as CTRL-D.

This EOF character isn't needed when you are reading in a file. This is because once you hit the end of your input stream, there is nothing left to "cin"(because the stream is now closed), thus the while loop exits.

Tags:

C++

File Io

Cin

Eof