问题
I wanted to ignore all characters in cin to flush cin in this answer: How to get rid of bad input one word at a time instead of one line at a time?
But I found that the program seemed to hang awaiting input if I wrote:
cin.ignore(std::numeric_limits<std::streamsize>::max());
It propperly flushed cin if I used the '\n' delimiter:
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
My question is, why can't I just ignore till EOF? Why do I have to provide the delimiter?
回答1:
The ignore function name is a little bit misleading. What it actually does it read and discard input until the terminator is found. And that's a blocking read.
In your case, whatever input stream you are using with cin (by default it is stdin) never delivers an end-of-file condition, so ignore's read/discard loop blocks forever.
来源:https://stackoverflow.com/questions/28633195/cannot-cin-ignore-till-eof