【问题标题】:Why does my program have an infinite-loop when I enter in a character?为什么我的程序在输入字符时会出现无限循环?
【发布时间】:2014-02-18 04:08:03
【问题描述】:
#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; 

 }

当我在我的答案中输入一个字符以输入数字时,当我被问到是否想再次玩时,我输入的字符不是 Y、y、N 或 n无限循环

【问题讨论】:

  • 你使用的是什么编译器,因为我无法在 VS2010 下重现它,即使在 ideone.com/C88qG3 中也是如此?
  • @herohuyongtao g++ 和我在终端做它

标签: c++ character infinite-loop do-while


【解决方案1】:

userInput 定义为int。当您尝试读取int 时,实际上流中的内容是char,它将失败(但char 仍在缓冲区中)。您必须清除错误状态并忽略错误输入:

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
}

【讨论】:

  • 在我的代码中使用这个代码我还没有走那么远。我只需要知道我的循环在哪里,所以我可以修复它
  • 那是是什么导致你的无限循环。您的输入无效,因此当您执行 while 条件时,它不会停止循环,将其发送到循环的顶部,输入已经在缓冲区中,因此它会一直重复...。
  • 如果我将 userInput 声明为“char userInput”,我仍然会得到一个无限循环
  • @user2988803 只需使用 Zac 建议的代码并保持代码的另一部分不变。无需将 userInput 更改为 char。您的 cin 语句必须在 if 条件内,仅此而已
【解决方案2】:

只是一个建议,但我会重新排列您的代码如下:

#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; 

 }

请注意我如何将char 输入转换为int。

如果您想使用当前循环将userInput 声明为char,然后使您的switch 语句如下:switch(userInput - '0')

【讨论】:

  • 我还没有深入编码以在我的代码中使用此代码。我只需要知道我的循环在哪里,所以我可以修复它
  • 我进行了编辑,但在它询问我是否要再次播放后,我无法退出程序......就像我输入 N 或 n 一样,我无法退出程序
  • @user2988803 试试我在代码中使用的 if 条件。
  • 当我输入 Y 或 y...它不会重播游戏
  • IM 也使用 g++ 编译器,我在终端中进行操作
猜你喜欢
  • 2018-11-06
  • 1970-01-01
  • 2023-03-22
  • 1970-01-01
  • 2021-06-17
  • 2016-08-01
  • 2013-10-31
  • 2018-08-21
相关资源
最近更新 更多