如何正确使用cin.clear cin.ignore [英] How to properly use cin.clear an cin.ignore

查看:139
本文介绍了如何正确使用cin.clear cin.ignore的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    int main()
{
    int oddeven = 1;
    int modulotwo;
    // Simple loop since no reason to check 0
    while(oddeven != 0){
        //user input
        cout << "\n\nIn while loop\n";
        cout << "Input number to determine if it is odd or even.\n";
        cin >> oddeven;
        //check to see if input succesful
        if(!(cin.fail())) {
            cout << "In if loop\n";
            cout << "Divide by two and put in modulotwo.\n";
            //check to see if odd or even by dividing by 2
            modulotwo = oddeven % 2;
            cout << "Output modulotwo: ";
            //visual confirmation of remainder
            cout << modulotwo << "\n";
            if (modulotwo == 0) {
                cout << "Check modulotwo against 0\n";
                cout << "The number " << oddeven << " is even.\n";

            }
            else {
                cout << "The number " << oddeven << " is odd.\n";

            }
        }
        //If input not succesful
        else {
            cout << "In else statement. Set oddeven to 1\n";
            //set oddeven to 1 to stop exit
            oddeven = 1;
            cout << "Not a usable number, please input integer number." << endl;
            cout << "Run cin.clear\n";
            cin.clear();
            cout << "Run cin.ignore\n";
            cin.ignore(std::numeric_limits<std::streamsize>::max());

        }
    }

}



<我上面有一个简单代码,用于检查数字是奇数还是偶数。我正在尝试向其添加错误更正,以确保输入的类型正确。我指着cin.clear cin.ignore。
我的问题是,如果引起错误,它将正确地放入else语句中,仅此而已。它将不再运行其余部分。我从cin.ignore假设它。
如何获取以正确清除错误并返回输入循环?
如果删除了cin.ignore,我会得到一个连续的循环,告诉我1是奇数而不等待任何输入。

I have above, a "simple" code to check whether a number was odd or even. I am attempting to add error correction to it to make sure the input is correct type. I was pointed to cin.clear an cin.ignore. My issue is, if I cause an error it correctly drops into the else statement, but that is it. It will no longer run the rest. I am assuming it from the cin.ignore. How do I get it to properly clear the error and return to the input loop? If I remove the cin.ignore, I get a continuous loop telling me that 1 is odd without waiting for any input.

多余的输出文本是我的试图跟踪代码在哪里以及代码在做什么的方法。

The extra output text was my way of trying to track where the code was and what it was doing.

编辑。在这里发布之前,我实际上已经将cin.ignore注释掉了。我试图自己研究问题,并遇到了有关该问题的评论,以寻找换行符,但正如大家都发现的那样,它不起作用。输入字母而不是任何数字(太大或其他数字)时,会出现问题。

Edit. I had actually had the cin.ignore commented out before posting here. I was trying to research the problem myself and had come across a comment about it looking for a newline character, but as you all have found out also, it doesn't work. The problem shows up when entering a letter rather than any number, too larger or otherwise.

推荐答案


我的问题是,如果我引起错误,它将正确地放入else语句中,但是这就对了。

My issue is, if I cause an error it correctly drops into the else statement, but that is it. It will no longer run the rest.

问题出在行中

cin.ignore(std::numeric_limits<std::streamsize>::max(), "\n");

第二个参数必须是字符,而不是字符串。

The second argument needs to be a character, not a string.

cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

提高编译器的警告级别,以检测此类错误。

Increase the warning level of your compiler to detect such errors.

使用 g ++ -std = c ++ 11 -Wall ,我得到以下警告/错误消息。

By using g++ -std=c++11 -Wall, I get the following warning/error message.

socc.cc: In function ‘int main()’:
socc.cc:43:70: error: invalid conversion from ‘const char*’ to ‘std::basic_istream<char>::int_type {aka int}’ [-fpermissive]
          cin.ignore(std::numeric_limits<std::streamsize>::max(), "\n");
                                                                      ^
In file included from /usr/include/c++/5/iostream:40:0,
                 from socc.cc:1:
/usr/include/c++/5/istream:657:5: note:   initializing argument 2 of ‘std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::ignore(std::streamsize, std::basic_istream<_CharT, _Traits>::int_type) [with _CharT = char; _Traits = std::char_traits<char>; std::streamsize = long int; std::basic_istream<_CharT, _Traits>::int_type = int]’
     basic_istream<char>::

这篇关于如何正确使用cin.clear cin.ignore的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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