I have an application that implements an interactive shell, similar to how the Python console / irb works. The problem now is that if the user accidentally hits ^D
EOF is issued and my getline()
call returns an empty string which i treat as "no input" and display the prompt again.
This then results in an endless loop that prints the prompt.
Now in Python I would solve that problem by catching EOFError
, but in C++ no exception is raised I could catch and there doesn't seem to be a setting on cin
to ignore EOF.
Any hints?
If it could not read anything, it sets the failbit
. Just test the stream in an if condition, and clear the bit:
if(!getline(std::cin, myline)) {
std::cin.clear();
std::cout << "you should enter something" << std::endl;
}
Internally, the sequence is this way in your case:
- Wait on the terminal for a string. Terminal will block until the user emits a newline. Two probable error cases possible
- User presses immediately EOF. This will make
getline
read nothing at all, and it will set the failbit
and the eofbit
.
- User inputs something and then presses EOF. This will make
getline
consume something and then it hits EOF while trying to get the next character. This cause eofbit
to be set.
- You will try to read something again. The extraction function will create an object of type
istream::sentry
which checks in what state the stream is. If any of the errorbits are set, it will cause the extraction function to immediately return. That caused the endless loop before.
A call to clear()
clears all error bits, and you can go on reading your stuff again.
Correct solution thanks to litb:
if (!getline(std::cin, str)) {
std::cin.clear();
std::cout << std::endl;
}
The getline()
function signals errors by using the following bits:
Try checking these before you proceed.
See http://www.horstmann.com/cpp/pitfalls.html
You can use code like:
while (cin)
{ int x;
cin >> x;
if (cin) a.push_back(x);
}
Ok, in other answers using cin.clear() was described as a possible solution.
Another trick is that you use other means to process the input from the console than the typical standard in by setting the terminal to another mode so you can process Ctrl+D directly. In RAW mode or others, you gain more direct access to the input and control sequences from the user side (like Ctrl+D or Ctrl+C) are not handled elswhere anymore.
Some libraries you may try to gather more information (or even save coding time):
- GNU Readline
- Useful Terminal I/O Library ½
- NCurses (which is very powerful)
½ You can find some information about your problem in the docs here.