-1

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!

  • Step 1: Make sure you check that the input was read succesfully. `cin >> Sel;` needs to be `if (cin >> Sel) { use Sel } else {cin.clear(); cin.ignore(); }` Note that you may need to ignore more than one character to flush bad inputs. Typically you blast the whole line, but this might be overkill. – user4581301 Apr 05 '19 at 21:04
  • @user4581301 Thanks for your response. I realized I phrased my question and post poorly so I went through and edited everything to ask a more clear and concise question. I'm more or less struggling with something conceptually in regards to how to use loops properly to force a user to choose one or two options. The post will obviously have a better break down. – ThatHippie Apr 05 '19 at 21:41
  • You may benefit from [A: How to catch invalid user inputs](https://stackoverflow.com/questions/54473804/how-to-catch-invalid-user-inputs/54474262?r=SearchResults&s=4|13.9010#54474262) or [A: how to clear the failbit while reading the data from input stream?](https://stackoverflow.com/questions/50227372/how-to-clear-the-failbit-while-reading-the-data-from-input-stream/50228738?r=SearchResults&s=1|21.7540#50228738) – David C. Rankin Apr 05 '19 at 21:42
  • @DavidC.Rankin Thank you. Neither of those links were a problem similar to mine, but it did lead me to a post which had the exact issue I faced. – ThatHippie Apr 05 '19 at 22:22

1 Answers1

0

The culprit is this line at the end on the do/while loop.

while (Sel != 'q' || Sel != 'Q');

Should be the following.

while (Sel != 'q' && Sel != 'Q');