cin条件检查错误 [英] cin condition checking error

查看:71
本文介绍了cin条件检查错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是学习c ++的初学者.我在使用cin命令时遇到麻烦.在下面的程序部分中,如果我在第一个cin命令中输入了错误的类型,则该程序将完全不执行以下任何cin命令,但将执行该程序的其余部分.

I am a beginner programmer learning c++. I am having a nagging issue with the cin command. In the program section below, if I enter a wrong type at the 1st cin command, the program will not execute any of the following cin commands at all, but will execute the rest of the program.

//start
#include <iostream>
using namespace std;
int main()
{
    int x=0;
    cout << endl << "Enter an integer" << endl;

    //enter integer here. If wrong type is entered, goes to else
    if (cin >> x){
            cout << "The value is " << x << endl;
    }
    else {
         cout << "You made a mistake" << endl; //executes
         cin.ignore();
         cin.clear();
    }
    cout << "Check 1" << endl; //executes
    cin >> x;                  //skips
    cout << "Check 2" << endl; //executes
    cin >> x;                  //skips
    return 0;                  
}
//end

如果我将相同的概念放在一个循环中,而不是其他而(!(cin >> x))输入错误的输入后,程序将进入无限循环.请帮助我解释这种现象,因为我正在遵循的教科书上说,上面键入的代码应该可以按预期运行.

Instead of the if else, if i put the same concept in a loop while (!(cin >> x)) the program goes into an infinite loop upon enterring a wrong input. Please help me explain this phenomenon, as the text book i am following says the code typed above should work as intended.

谢谢

推荐答案

cin 是输入流.如果发生错误, cin 进入发生错误"状态.在这种状态下,无法进行任何字符输入,但是将忽略您从输入流中收集字符的请求.使用 clear()清除错误,输入流不再忽略您.

cin is an input stream. If an error occurs cin goes into a let's call it "error occured" state. While in this state no character input can be made, your request to collect a character from the input stream will be ignored. With clear() you clear the error and the input stream stops ignoring you.

这是忽略函数原型

istream&  ignore ( streamsize n = 1, int delim = EOF );

此函数从输入流中获取字符并将其丢弃,但是如果您的流忽略了您,您将无法获取任何字符,因此您必须先 clear()然后再 ignore().

This function gets characters from the input stream and discards them, but you can't get any character if your stream is ignoring you, so you have to first clear() the stream then ignore() it.

此外,在侧面还有一个注释:如果有人输入,例如"abc" ,则在第一个输入请求中,您的 cin 仅获得一个字符,该字符为'a'"bc" 留在缓冲区中等待被拾取,但是下一次调用 cin 时得到的是'b''c'保留在缓冲区中,因此您再次遇到错误.

Also, a note on the side: If someone inputs, for example "abc", on the first input request your cin gets only one character that is 'a' and "bc" stays in the buffer waiting to be picked up, but the next call to cin gets the 'b' and 'c' stays in the buffer, so you again end up with an error.

此示例的问题在于,如果未传递任何参数,则 cin.ignore()仅会在您 clear()之后忽略1个字符.第二个 cin 获得'c',因此您仍然遇到问题.

The problem with this example is that the cin.ignore() if no arguments are handed to it only ignores 1 character after you clear(). and the second cin gets 'c' so you still have a problem.

解决此问题的一般方法是致电

A general solution to this problem would be to call

cin.ignore(10000, '\n');

第一个数字必须是您不希望有人输入的巨大数字,我通常输入10000.

The first number just has to be some huge number that you don't expect someone would enter, I usually put in 10000.

此调用可确保您从错误输入中拾取所有字符,或者确保在按下Enter键之前拾取所有字符,以使您的输入流不会两次进入发生错误"状态.

This call makes sure that you pick up all the characters from the false input or that you pick up every character before the enter was pressed so your input stream doesn't get into the "error occurred" state twice.

这篇关于cin条件检查错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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