直到EOF不能cin.ignore? [英] Cannot cin.ignore till EOF?
问题描述
我想忽略 cin
中的所有字符以清除此答案中的 cin
:如何摆脱一次一个字的坏输入,而不是一次一行?
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());
如果我使用了 cin
'\\\
分隔符:
'
It propperly flushed cin
if I used the '\n'
delimiter:
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
我的问题是,为什么我不能忽略到EOF?为什么必须提供分隔符?
My question is, why can't I just ignore till EOF? Why do I have to provide the delimiter?
推荐答案
忽略
函数名称有点误导。它实际上读取和丢弃输入,直到找到终止符。
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.
在你的情况下,你使用的任何输入流 cin
是 stdin
)从不提供文件结束条件,因此忽略
的读/丢弃循环块永远。
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.
这篇关于直到EOF不能cin.ignore?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!