小编Log*_*aso的帖子

我的cin在while循环中被忽略了

我正在尝试将一个简单的问题和数字检查器编码到我的第一个C++程序中.问题是,当我输入一个像两个或三个字符串的字符串时,程序变为无限循环,它忽略了cin函数,将生命重新分配给一个数字.

cout << "How many lives would you like 1 (hard), 2 (medium), or 3 (easy)?" << endl;
cin >> lives;


while(lives != 1 && lives != 2 && lives != 3 && !isdigit(lives))
{
    cout << "You need to input a number, not words." << endl;
    cout << "How many lives would you like 1 (hard), 2 (medium), or 3 (easy)?" << endl;
    cin >> lives;
}
Run Code Online (Sandbox Code Playgroud)

以下是我当前的代码以及您的建议:

    cout << "How many lives would you like 1 (hard), 2 …
Run Code Online (Sandbox Code Playgroud)

c++ cin infinite-loop while-loop

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

标签 统计

c++ ×1

cin ×1

infinite-loop ×1

while-loop ×1