Do-While loop invalid input issue

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 . . .

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cmath>
using namespace std;

int main()
{
    int userMove = 1;
    int gameNum = 1;
    srand(static_cast<unsigned int> (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.
Last edited on
Topic archived. No new replies allowed.