cin为一个int输入一个字符原因循环,应该检查输入野 [英] cin for an int inputing a char causes Loop that is supposed to check input to go wild

查看:224
本文介绍了cin为一个int输入一个字符原因循环,应该检查输入野的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  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>>艾奥瓦尔
cout<< endl;

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

{
cout< KeingültigesFeld bittewählensie noch einmal!\\\
< endl;
cin>>艾奥瓦尔
}
feldfuellen(iAuswahl,cXO);
}


解决方案

从流中,错误标志被设置,并且不能再读取,直到您清除错误标志。这就是为什么你得到一个无限循环。

  cin.clear(); //清除错误标志
//这行丢弃所有在流中等待的输入
cin.ignore(std :: numeric_limits< std :: streamsize> :: max(),'\\\
');

此外,如果不知道读取是否成功,使用输入操作的结果是错误的首先。您不能假设 iAuswahl 的值。这是由新手使用流最常犯的错误之一。始终检查输入操作是否正常。这最容易通过在布尔上下文中使用 operator>> 来完成:

  if(cin>> some_obj){
//如果成功
,则求值为true} else {
//出错了
}



而且,我的哦,这一行

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


b $ b

可以是这样:

  while(iAuswahl< 1 || iAuswahl> 9)

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

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


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.\n" << 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!\n" << 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(), '\n');

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
}

And, my oh my, this line

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

can be just this:

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\n";
    cin.clear();
    cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}

这篇关于cin为一个int输入一个字符原因循环,应该检查输入野的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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