C++:如何检查 cin 缓冲区是否为空?
如何检查用户是否没有在 cin 命令中输入任何内容而只是按了 Enter?
How do you check to see if the user didn't input anything at a cin command and simply pressed enter?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
从 std::cin 读取时,最好不要使用 流提取运算符
>>
因为这可以有各种令人讨厌的副作用。例如,如果您有以下代码:我输入
John Doe
,然后从cin
读取的行将仅保存值John
,留下Doe
后面由一些未来的读取操作读取。同样,如果我要写:然后我输入
John Doe
,那么cin
将进入错误状态,并拒绝执行任何未来的读取操作,直到您明确清除它的错误状态并刷新导致错误的字符。进行用户输入的更好方法是使用 std::getline 来从键盘读取字符,直到用户按下 Enter 键。例如:
ADL 代表参数相关查找。现在,如果我输入
John Doe
,name
的值将为John Doe
,并且name
中不会留下任何数据>cin。此外,这还可以让您测试用户是否只是按回车键:使用此方法的缺点是,如果您想读取格式化的数据行,则需要读取
int
或double 您必须从字符串中解析表示形式。我个人认为这是值得的,因为它可以让您更细粒度地控制用户输入无效内容时要做什么,并“保护”
cin
免于进入失败状态。我教授 C++ 编程课程,并且有一些关于流库的讲义< /a> 详细介绍了如何以安全的方式从 cin 读取格式化数据(主要在本章末尾)。我不确定您会发现这个有多大用处,但如果它有帮助,我想我会发布链接。
希望这有帮助!
When reading from std::cin, it's preferable not to use the stream extraction operator
>>
as this can have all sorts of nasty side effects. For example, if you have this code:And I enter
John Doe
, then the line to read fromcin
will just hold the valueJohn
, leavingDoe
behind to be read by some future read operation. Similarly, if I were to write:And I then type in
John Doe
, thencin
will enter an error state and will refuse to do any future read operations until you explicitly clear its error state and flush the characters that caused the error.A better way to do user input is to use std::getline to read characters from the keyboard until the user hits enter. For example:
ADL stands for argument-dependent lookup. Now, if I enter
John Doe
, the value ofname
will beJohn Doe
and there won't be any data left around incin
. Moreover, this also lets you test if the user just hit enter:The drawback of using this approach is that if you want to read in a formatted data line, an
int
or adouble
you'll have to parse the representation out of the string. I personally think this is worth it because it gives you a more fine-grained control of what to do if the user enters something invalid and "guards"cin
from ever entering a fail state.I teach a C++ programming course, and have some lecture notes about the streams library that goes into a fair amount of detail about how to read formatted data from
cin
in a safe way (mostly at the end of the chapter). I'm not sure how useful you'll find this, but in case it's helpful I thought I'd post the link.Hope this helps!
除非用户输入至少 1 个字符(输入不算),否则 cin 将不会继续执行该程序。如果用户没有给出任何输入,cin 将继续等待用户给出输入,然后按 Enter。
cin will not continue with the program unless the user enters at least 1 character (enter doesn't count). If the user doesn't give ANY input, cin will just keep waiting for the user to give input and then press enter.
简单的方法>>
但一个简单的问题是......
,我的意思是数字或字母或特殊符号,而不是空格,输入 null 等
希望这可以解决您的问题,如果没有,我会很高兴帮忙让我知道。
The Simple way >>
But a simple problem is....
by character here i mean a DIGIT or alphabet or special symbol , not space, enter null etc
Hope this solves your problem, if it doesn't, I'll be glad to help just let me know.
有任何问题请告诉我。
Any problem let me know.