小编Jen*_*ger的帖子

cin for int输入一个char会导致应该检查输入的循环变为疯狂

这是我的游戏的功能,它会要求输入和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.\n" << endl;
    grafik();
    cout << "Sie sind >> " << cXO << " <<." << endl;
    cin >> iAuswahl;
    cout << endl;

    while ( 
        iAuswahl != 1 
        && iAuswahl != 2 
        && iAuswahl != 3 
        && iAuswahl != 4 
        && iAuswahl != 5 
        && …
Run Code Online (Sandbox Code Playgroud)

c++ int buffer cin char

5
推荐指数
1
解决办法
1万
查看次数

标签 统计

buffer ×1

c++ ×1

char ×1

cin ×1

int ×1