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?
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:
Correct solution thanks to litb:
If it could not read anything, it sets the
failbit
. Just test the stream in an if condition, and clear the bit:Internally, the sequence is this way in your case:
getline
read nothing at all, and it will set thefailbit
and theeofbit
.getline
consume something and then it hits EOF while trying to get the next character. This causeeofbit
to be set.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.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):
½ You can find some information about your problem in the docs here.