Hey all,
I have this rock paper scissors code that keeps repeating while the user picks 1 2 or 3. Pick a letter get error code and exit, pick another number get error code and exit. However if you enter say 1w it will output what is below so I am having trouble understanding why cin takes 1w as a 1 then a failed entry displaying the results from 1 then error and exit. Also I am purposely trying to use only switch but if statements give the same issues. Any tips or insight is greatly appreciated
Game Number 1
(1) for Scissors
(2) for Rock
(3) for paper
1w
You chose scissors, Hal chose rock. You lose.
Game Number 2
(1) for Scissors
(2) for Rock
(3) for paper
Invalid input
Press any key to continue . . .
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cmath>
usingnamespace std;
int main()
{
int userMove = 1;
int gameNum = 1;
srand(static_cast<unsignedint> (time(0))); // random num seed
do
{
int aiMove = rand() % 3 + 1;
cout << endl << "Game Number " << gameNum++ << endl; //ticker for game number
cout << "(1) for Scissors" << endl << "(2) for Rock"
<< endl << "(3) for paper " << endl;
cin.clear();
cin >> userMove;
while (std::cin.fail()) // ends loop when wrong data
{ // type is entered
cout << "Invalid input" << endl;
return 0;
}
switch (userMove) // main switch for user's pick
{
case 1:
switch (aiMove) //compares user to ai move decides winner
{
case 1:
cout << "You chose scissors, Hal chose scissors."
<< " It is a draw." << endl;
break;
case 2:
cout << "You chose scissors, Hal chose rock."
<< " You lose." << endl;
break;
case 3:
cout << "You chose scissors, Hal chose is paper."
<< " You win." << endl;
break;
default:
cout << "ERROR: This shouldn't happen" << endl;
break;
}
break;
case 2:
switch (aiMove) //compares user to ai move decides winner
{
case 1:
cout << "You chose rock. Hal chose scissors."
<< " You win." << endl;
break;
case 2:
cout << "You chose rock, Hal chose rock."
<< " It is a draw." << endl;
break;
case 3:
cout << "You chose rock, Hal chose paper."
<< " You lose." << endl;
break;
default:
cout << "ERROR: Hal messed up some how" << endl;
break;
}
break;
case 3:
switch (aiMove) //compares user to ai move decides winner
{
case 1:
cout << "You chose paper. Hal chose scissors."
<< " You lose." << endl;
break;
case 2:
cout << "You chose paper, Hal chose rock."
<< " You win." << endl;
break;
case 3:
cout << "You chose paper, Hal chose paper."
<< " It is a draw." << endl;
break;
}
break;
default:
cout << "Invalid input" << endl;
return 0;
}
} while (userMove == 1 || 2 || 3); //while input is valid new game starts
return 0;
}
I am having trouble understanding why cin takes 1w as a 1 then a failed entry displaying the results from 1 then error and exit.
userMove is an int. cin will process the input buffer for characters that can be converted to an int. It will stop when it reaches a character that can not be converted to an int (w), leaving that character in the input buffer The next cin to an int fails because the next character in the input buffer (w) can not be converted to an int. When you detect a cin.fail(), you should do both a cin.clear() and a cin.ignore().
BTW, line 94 is bogus.
C++ does not support implied left hand side in conditionals. You must fully specify the conditions.
Example: if (ans == 'Y' || 'y') evaluates as if ((ans == 'Y') || ('y'))
('y') always evaluates to 1 (true), therefore the if statement is always true.