直到EOF不能cin.ignore? [英] Cannot cin.ignore till EOF?

查看:283
本文介绍了直到EOF不能cin.ignore?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想忽略 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆