I am trying to learn basic C++ this summer and I am struggling with this Rock, Paper, Scissors game program.
For some reason, when I run the program, it only works every other time the do-while loop iterates. So the first time the program executes correctly, then the second time the menu choices print but when the user answers, it just displays the menu again. Then it works on the 3rd iteration, but not the 4th time, and so on.
So the getUserChoice function seems to get called and executes each time, but maybe not the determineWinner function?
I have tried a lot of different changes but I cannot seem to find the bug. Any help would be greatly appreciated!
int getComputerChoice(int computerChoice)
{
//declare vars for min and max of random number
const int MIN = 1;
const int MAX = 3;
//get system time
unsigned seed = time(0);
//randomize rand
srand(seed);
// Generate random number
computerChoice = MIN + rand() % MAX;
return computerChoice;
}
int getUserChoice(int userChoice)
{
//declare constants for menu choices
const int ROCK = 1, PAPER = 2, SCISSORS = 3, QUIT = 4;
cout<<"Rock, Paper, Scissors Game\n"
<<"---------\n"
<<"1) Rock\n"
<<"2) Paper\n"
<<"3) Scissors\n"
<<"4) Quit\n\n"
<<"Enter your choice:\n";
cin>>userChoice;
//validate input
while (userChoice <1 || userChoice>4)
{
cout<<"Invalid selection. Enter 1, 2, 3, or 4:\n";
cin>>userChoice;
}
return userChoice;
}
void determineWinner(int userChoice, int computerChoice)
{
if(userChoice == 1 && computerChoice == 2)
{
cout<<"\nYou selected: ROCK\n\n"
<<"The computer selected: PAPER\n\n"
<<"Computer wins! Paper wraps rock!\n\n"
<<"*********************************\n"<<endl;
}
else if(userChoice == 1 && computerChoice == 3)
{
cout<<"\nYou selected: ROCK\n\n"
<<"The computer selected: SCISSORS\n\n"
<<"You win! Rock smashes scissors!\n\n"
<<"*********************************\n"<<endl;
}
else if(userChoice == 2 && computerChoice == 1)
{
cout<<"\nYou selected: PAPER\n\n"
<<"The computer selected: ROCK\n\n"
<<"You win! Paper wraps rock!\n\n"
<<"*********************************\n"<<endl;
}
else if(userChoice == 2 && computerChoice == 3)
{
cout<<"\nYou selected: PAPER\n\n"
<<"The computer selected: SCISSORS\n\n"
<<"Computer wins! Scissors cut paper!\n\n"
<<"*********************************\n"<<endl;
}
else if(userChoice == 3 && computerChoice == 1)
{
cout<<"\nYou selected: SCISSORS\n\n"
<<"The computer selected: ROCK\n\n"
<<"Computer wins! Rock smashes scissors!\n\n"
<<"*********************************\n"<<endl;
}
else if(userChoice == 3 && computerChoice == 2)
{
cout<<"\nYou selected: SCISSORS\n\n"
<<"The computer selected: PAPER\n\n"
<<"You win! Scissors cut paper!\n\n"
<<"*********************************\n"<<endl;
}
else
cout<<"\nTie, No winner!\n\n"
<<"*********************************\n"<<endl;
}
int main()
{
//declare vars
int userChoice, computerChoice;
do
{
//call determineWinner function
determineWinner(getUserChoice(userChoice), getComputerChoice(computerChoice));
}
while (getUserChoice(userChoice) != 4);
system ("pause");
return 0;
}