C++:如何检查 cin 缓冲区是否为空?

发布于 2024-10-17 16:31:44 字数 44 浏览 0 评论 0原文

如何检查用户是否没有在 cin 命令中输入任何内容而只是按了 Enter?

How do you check to see if the user didn't input anything at a cin command and simply pressed enter?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

醉态萌生 2024-10-24 16:31:44

std::cin 读取时,最好不要使用 流提取运算符 >> 因为这可以有各种令人讨厌的副作用。例如,如果您有以下代码:

std::string name;
std::cin >> name;

我输入 John Doe,然后从 cin 读取的行将仅保存值 John,留下 Doe后面由一些未来的读取操作读取。同样,如果我要写:

int myInteger;
std::cin >> myInteger;

然后我输入 John Doe,那么 cin 将进入错误状态,并拒绝执行任何未来的读取操作,直到您明确清除它的错误状态并刷新导致错误的字符。

进行用户输入的更好方法是使用 std::getline 来从键盘读取字符,直到用户按下 Enter 键。例如:

std::string name;
getline(std::cin, name); // getline doesn't need the std:: prefix here because C++ has ADL.

ADL 代表参数相关查找。现在,如果我输入 John Doename 的值将为 John Doe,并且 name 中不会留下任何数据>cin。此外,这还可以让您测试用户是否只是按回车键:

std::string name;
getline(std::cin, name);

if (name.empty()) {
    /* ... nothing entered ... */
}

使用此方法的缺点是,如果您想读取格式化的数据行,则需要读取 intdouble 您必须从字符串中解析表示形式。我个人认为这是值得的,因为它可以让您更细粒度地控制用户输入无效内容时要做什么,并“保护”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:

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:

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.

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:

std::string name;
getline(std::cin, name); // getline doesn't need the std:: prefix here because C++ has ADL.

ADL stands 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:

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.

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!

靑春怀旧 2024-10-24 16:31:44

除非用户输入至少 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.

寻梦旅人 2024-10-24 16:31:44

简单的方法>>

{
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 !! ";
}

但一个简单的问题是......

cin 只是不允许您在不输入字符的情况下进一步移动

,我的意思是数字或字母或特殊符号,而不是空格,输入 null 等

希望这可以解决您的问题,如果没有,我会很高兴帮忙让我知道。

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

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.

染火枫林 2024-10-24 16:31:44
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
}
}

有任何问题请告诉我。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文