Cannot cin.ignore till EOF?

丶灬走出姿态 提交于 2019-12-23 03:07:54

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!