Jonathan Mee
Jonathan Mee

Reputation: 38919

Cannot cin.ignore till EOF?

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?

Upvotes: 3

Views: 1195

Answers (1)

Ben Voigt
Ben Voigt

Reputation: 283624

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.

Upvotes: 6

Related Questions