C++:如何检查 cin 缓冲区是否为空?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4999650/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++: how do I check if the cin buffer is empty?
提问by Matt Shindala
How do you check to see if the user didn't input anything at a cin command and simply pressed enter?
您如何检查用户是否没有在 cin 命令中输入任何内容并简单地按下 Enter 键?
回答by templatetypedef
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:
从std::cin读取时,最好不要使用流提取运算符,>>
因为这会产生各种令人讨厌的副作用。例如,如果您有以下代码:
std::string name;
std::cin >> name;
And I enter John Doe
, then the line to read from cin
will just hold the value John
, leaving Doe
behind to be read by some future read operation. Similarly, if I were to write:
然后我输入John Doe
,然后要读取的cin
行将只保存值John
,留下Doe
供将来的某些读取操作读取。同样,如果我要写:
int myInteger;
std::cin >> myInteger;
And I then type in John Doe
, then cin
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.
然后我输入John Doe
,然后cin
将进入错误状态并拒绝执行任何未来的读取操作,直到您明确清除其错误状态并刷新导致错误的字符。
A better way to do user input is to use std::getlineto read characters from the keyboard until the user hits enter. For example:
进行用户输入的更好方法是使用std::getline从键盘读取字符,直到用户按 Enter 键。例如:
std::string name;
getline(std::cin, name); // getline doesn't need the std:: prefix here because C++ has ADL.
ADLstands for argument-dependent lookup. Now, if I enter John Doe
, the value of name
will be John Doe
and there won't be any data left around in cin
. Moreover, this also lets you test if the user just hit enter:
ADL代表参数依赖查找。现在,如果我输入John Doe
, 的值name
将会是John Doe
并且不会有任何数据留在cin
. 此外,这还可以让您测试用户是否只是按 Enter 键:
std::string name;
getline(std::cin, name);
if (name.empty()) {
/* ... nothing entered ... */
}
The drawback of using this approach is that if you want to read in a formatted data line, an int
or a double
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.
使用这种方法的缺点是,如果您想读取格式化的数据行、 anint
或 a double
,则必须从字符串中解析出表示形式。我个人认为这是值得的,因为它可以让您更精细地控制如果用户输入无效的内容并“cin
防止”进入失败状态,则该做什么。
I teach a C++ programming course, and have some lecture notes about the streams librarythat 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.
我教授 C++ 编程课程,并有一些关于流库的讲义,其中详细介绍了如何以cin
安全的方式读取格式化数据(主要在本章末尾)。我不确定你会发现它有多大用处,但如果它有帮助,我想我会发布链接。
Hope this helps!
希望这可以帮助!
回答by matzahboy
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.
cin 不会继续执行程序,除非用户输入至少 1 个字符(输入不算数)。如果用户没有提供任何输入,cin 将一直等待用户提供输入,然后按 Enter。
回答by Kartikya
The Simple way >>
简单的方法>>
{
char X=0; // ASCII( 0 ) means a NULL value
cin>>X;
if(X==0 || X==10) // ASCII( 10 ) means ENTER
cout<<"User din't enter ANYTHING !! ";
}
But a simple problem is....
但一个简单的问题是......
cin just won't allow you to move further without entering a character
cin 只是不允许您在不输入字符的情况下进一步移动
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.
希望这能解决您的问题,如果没有,我会很乐意帮助您告诉我。
回答by JesusChrist
int main(){
string str[100];
std::cout<<"Hello how are you ? \n";
std::cin>>str;
if(str.length() > 0){
// If input is seen
}
else{
// If input is not seen
}
}
Any problem let me know.
有任何问题让我知道。