Reputation: 32533
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?
Upvotes: 3
Views: 4647
Reputation: 506905
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:
getline
read nothing at all, and it will set the failbit
and the eofbit
. getline
consume something and then it hits EOF while trying to get the next character. This cause eofbit
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.
Upvotes: 7
Reputation: 32533
Correct solution thanks to litb:
if (!getline(std::cin, str)) {
std::cin.clear();
std::cout << std::endl;
}
Upvotes: 3
Reputation: 52337
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.
Upvotes: 0
Reputation: 21012
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);
}
Upvotes: 0
Reputation: 111130
The getline()
function signals errors by using the following bits:
Try checking these before you proceed.
Upvotes: 1