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

PakkuDon
  • 1,627
  • 4
  • 22
  • 21
user2988803
  • 11
  • 1
  • 7

2 Answers2

3

userInput is defined as an 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). 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
}
Zac Howland
  • 15,777
  • 1
  • 26
  • 42
  • I haven't gone that far into coding to use this code in my code. I just need to know where my loop is, so I can fix it – user2988803 Feb 18 '14 at 04:17
  • 1
    That **is** what is causing your infinite loop. You have invalid input, so when you do your while condition, it does not stop the loop, sends it to the top of the loop, input is already in the buffer, so it repeats ... forever. – Zac Howland Feb 18 '14 at 04:50
  • If i declare userInput as "char userInput" i still get an infinte loop – user2988803 Feb 18 '14 at 05:01
  • @user2988803 Just use the code that Zac suggested and keep the other part of the code as it is. No need of changing userInput to char. Your cin statement has to be inside the if condition, that's all – sajas Feb 18 '14 at 06:04
1

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.

If you want to use your current loop declare userInput as a char and then make your switch statement like this: switch(userInput - '0')

turnt
  • 3,235
  • 5
  • 23
  • 39