Overall problem I'm having: I'm misunderstanding how to properly validate user input in a menu selection using loops/if-else-if sort of logic. I need to figure out what I'm doing conceptually wrong, more than technically wrong (as we have very specific rules of how to approach our programs in class).
Problem #1 = When the program starts there are two options to the user. There obviously needs to be a response that a user has put in a invalid character if neither of the two options is picked. My problem is that when I’m on the invalid selection screen it only allows me to hit ‘P’ to start the game, and not ‘Q’ to end the game. Something in my loop is only accepting ‘P’ as valid input to push the program forward.
I've tried switching between do/while loops and if/else if statements. The do/while loop has provided the least amount of problems, while still retaining some.
I'm only in my second programming class so I probably can't give a better detailed answer than I've tried different loops and moved small bits of code around, sorry.
I’m only going to post the int main() of the program as that contains the menu i’m having problems with. There are additional functions that have user input with similar issues, but if I fix my conceptual mistake here, I can fix it there after.
int main()
{
char Sel;
int compChoice;
int playerChoice;
cout << "\n";
cout << "ROCK PAPER SCISSORS MENU" << endl;
cout << "------------------------" << endl;
cout << "p) Play Game" << endl;
cout << "q) Quit" << endl;
cout << "Please enter your choice:" << endl;
cin >> Sel;
if (Sel == 'q' || Sel == 'Q')
{
cout << "You have chosen to exit, thanks for playing" << endl;
}
do
{
if (Sel == 'p' || Sel == 'P')
{
playerChoice = getPlayerChoice();
compChoice = getComputerChoice();
if (isPlayerWinner(compChoice, playerChoice))
{
cout << "Winner Winner Chicken dinner" << endl;
}
else if (!isPlayerWinner(compChoice, playerChoice))
{
cout << "You lose this round" << endl;
}
else if (isTie(compChoice, playerChoice))
{
cout << "You have Tied" << endl;
}
else
{
cout << "Thanks for playing" << endl;
return 0;
}
}
else
{
cout << "Invalid selection, please try again by hitting 'p' or 'q'." << endl;
cin >> Sel;
}
} while (Sel != 'q' || Sel != 'Q');
system("PAUSE");
return 0;
}
Thanks for your time!