cin 用于输入 char 的 int 导致应该检查输入的循环变得疯狂 [英] cin for an int inputing a char causes Loop that is supposed to check input to go wild

查看:19
本文介绍了cin 用于输入 char 的 int 导致应该检查输入的循环变得疯狂的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的游戏的一个功能,它会要求输入和 cin 进入iAuswahl"!然后while循环检查它是否是我想要1-9的值之一,如果不是,它会激活并且应该要求新的输入.女巫它为int做.但是,如果我输入一个像 r 这样的字符,它会发疯,然后继续给我我的 cout 并跳过 cin!我的问题是它为什么会这样做,我该如何阻止它?

This is a function of my game it will ask for input and cin into "iAuswahl"! Then the while loop is checks if it is one of the Values i want 1-9 if not it activates and is supposed to ask for new input. Witch it does for int. But if i input a char like r it will go crazy and just keep giving me back my cout and skip the cin! My questions are why does it do it and how do i stop it?

void zug(string sSpieler, int iDran){
    int iAuswahl;
    char cXO = 'O';

    if (iDran == 1)
    {
        cXO = 'X';
    }

    cout << sSpieler << ", Sie sind am Zug. Bitte waehlen sie eins der Felder.
" << endl;
    grafik();
    cout << "Sie sind >> " << cXO << " <<." << endl;
    cin >> iAuswahl;
    cout << endl;

    while ( 
        iAuswahl != 1 
        && iAuswahl != 2 
        && iAuswahl != 3 
        && iAuswahl != 4 
        && iAuswahl != 5 
        && iAuswahl != 6 
        && iAuswahl != 7
        && iAuswahl != 8 
        && iAuswahl != 9
    )
    {
        cout << "Kein gültiges Feld bitte wählen sie noch einmal!
" << endl;
        cin >> iAuswahl;
    }
    feldfuellen(iAuswahl, cXO);
}

推荐答案

当从流中读取时发生错误时,会设置错误标志,并且在清除错误标志之前无法进行更多读取.这就是你得到一个无限循环的原因.

When an error occurs when reading from a stream, an error flag gets set and no more reading is possible until you clear the error flags. That's why you get an infinite loop.

cin.clear(); // clears the error flags
// this line discards all the input waiting in the stream
cin.ignore(std::numeric_limits<std::streamsize>::max(), '
');

另外,如果一开始不知道读取是否成功,使用输入操作的结果是错误的.您不能对 iAuswahl 的价值做出假设.这是新手使用流时最常犯的错误之一.经常检查输入操作是否正常.这很容易通过在布尔上下文中使用 operator>> 来完成:

Also, it's wrong to use the results of input operation if you don't know whether the read succeeded in the first place. You can't make assumptions about the value of iAuswahl. That's one of most often made errors by newbies using streams. Always check if the input operation was ok. This is most easily done by using operator>> in boolean context:

if (cin >> some_obj) {
    // evaluates to true if it succeeded
} else {
    // something went wrong
}

还有,天哪,这条线

while (iAuswahl != 1 && iAuswahl != 2 && iAuswahl != 3 && iAuswahl != 4 && iAuswahl != 5 && iAuswahl != 6 && iAuswahl != 7 && iAuswahl != 8 && iAuswahl != 9)

可以是这样的:

while (iAuswahl < 1 || iAuswahl > 9)

正确的循环可能如下所示:

A correct loop could look something like this:

while (true)
{
    if ((cin >> iAuswahl) && (iAuswahl >= 1) && (iAuswahl <= 9)) break;
    std::cout << "error, try again
";
    cin.clear();
    cin.ignore(std::numeric_limits<std::streamsize>::max(), '
');
}

这篇关于cin 用于输入 char 的 int 导致应该检查输入的循环变得疯狂的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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