简体   繁体   English

输入字符时,为什么程序会有无限循环?

[英]Why does my program have an infinite-loop when I enter in a character?

#include<iostream>
#include<cstdlib>
#include<ctime>
#include<string>

using namespace std;

int main()
{
    char replay;
    int userInput;
    cout<< "Let's play Rock, Paper, Scissors"<<endl;
  do
 { 
    cout<<"Enter 1 for Rock, 2 for Paper, 3 for Scissors"<< endl;
    cin>> userInput;

    switch(userInput)
    {
      case 1:
      cout <<"You chose rock" << endl;
      break;

      case 2:
      cout <<"You chose paper" <<endl;
      break;

      case 3:
      cout <<"You chose scissors" << endl;
      break;

      default:
      cout << userInput << " is not a valid choice"<< endl;
      break;
   }  
    cout<<"Would you like to play again (Y for yes, N for no)?"<<endl;
    cin >> replay;
 }  while((replay=='Y') || (replay=='y')); 

    return 0; 

 }

When I enter in a character in my answer for entering a number and when I'm asked if I want to play again and I enter in a character that isn't Y, y, N, or n it goes into an infinite loop 当我在输入数字的答案中输入一个字符时,当系统询问我是否要再次玩并且输入的字符不是Y,y,N或n时,它将陷入无限循环

userInput is defined as an int . userInput定义为int When you attempt to read an int , and what is actually in the stream is a char , it will fail (but the char is still in the buffer). 当您尝试读取一个int ,而流中实际包含的是char ,它将失败(但char仍在缓冲区中)。 You must clear the error status and ignore the bad input: 您必须清除错误状态并忽略错误的输入:

if (!(cin >> userInput))
{
    cin.clear(); // clears the error state
    cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // remove the bad input from the buffer
}
else
{
    // the code if the input was valid
}

Just a suggestion, but I would rearrange your code as follows: 只是一个建议,但我会按以下方式重新排列您的代码:

#include<iostream>
#include<cstdlib>
#include<ctime>
#include<string>

using namespace std;

int main()
{
    char replay;
    char userInputChar;
    int userInput;
    cout<< "Let's play Rock, Paper, Scissors"<<endl;
    for(;;)
    { 
        cout << "Enter 1 for Rock, 2 for Paper, 3 for Scissors"<< endl;
        cin >> userInputChar;

        userInput = userInputChar - '0';

        switch(userInput)
        {
            case 1:
            cout <<"You chose rock" << endl;
            break;

            case 2:
            cout <<"You chose paper" <<endl;
            break;

            case 3:
            cout <<"You chose scissors" << endl;
            break;

            default:
            cout << userInput << " is not a valid choice"<< endl;
            break;
        }  
        cout<<"Would you like to play again (Y for yes, N for no)?"<<endl;
        cin >> replay;

        if((replay!='Y') || (replay!='y'))
            break;
    } 

    return 0; 

 }

Notice how I took care of converting the char input to an int . 请注意,我是如何将char输入转换为int

If you want to use your current loop declare userInput as a char and then make your switch statement like this: switch(userInput - '0') 如果要使用当前循环, userInput声明为char ,然后使switch语句如下: switch(userInput - '0')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我的 char 变量达到 [del] 字符 (127) 值时,为什么我的程序会进入无限循环? - Why does my program enters into an infinite loop when my char variable reaches the [del] character (127) value? 每当我运行它时,我的程序就会进入无限循环,但只有当我输入无效数字然后输入字符或字符串时 - My program goes into an infinite loop whenever I run it, but only whenever I enter an invalid number then a character or string 为什么我的程序go会陷入死循环? - Why does my program go into an infinite loop? 为什么当我输入哨兵值时我的程序会崩溃? - Why does my program crash when I enter sentinel value? 错误:将元素加载到向量中时出现死循环 - Error: Infinite-loop when loading elements into vector 为什么我的代码在执行的初始嵌套 for 循环中进入无限循环? - Why does my code enter into an infinite loop at the initial nested for loop on execution? 为什么我的循环是无限的? 我用 91 测试过 - Why is my loop infinite? I have tested with 91 当我输入大量数字时,为什么我的计算器程序会开始闪烁和滚动 - why does my calculator program start flashing and scrolling when i enter a large number C++ 无限循环,返回问题 - C++ Infinite-loop, Issue with returning 为什么这个所谓的无限循环程序会终止? - Why does this supposedly infinite loop program terminate?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM