Rock, Paper Scissors using switch statement - What am I doing wrong?

Just trying to do a pretty simple Rock, Paper, Scissors game - Not sure what I'm doing wrong, but whenever I input '0', the game sort of works, but '1' and '2' just leave it blank.

Thanks in advance.

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
#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;

int computerChoice(){

srand(time(0));
int compChoice = 1 + (rand()%3);

return compChoice;

}

int main()
{
    int playerChoice = 0;

    cout << "Hello, and welcome to Rock, Paper, Scissors!\n\nRules of the game are: \n\n1) Rock beats scissors\n2) Scissors beats paper\n3) Paper beats rock.\n";
    cout <<"\nPlease make your choice: \n\nPick 0 for rock, 1 for paper and 2 for scissors (Press -1 to exit)." << endl;
    cin >> playerChoice;

    int compNumber = computerChoice();

    switch(compNumber){

    case 0:

        if (playerChoice == 0)
            cout << "\nYou picked rock, and the computer picked rock. The game is tied!\n";
        break;
        if (playerChoice == 1)
            cout << "\nYou picked paper, and the computer picked rock. You win!\n";
        break;
        if (playerChoice == 2)
            cout << "\nYou picked scissors, and the computer picked rock. You lose!\n";
        break;

    case 1:

        if (playerChoice == 0)
            cout << "\nYou picked rock, and the computer picked paper. You lose!\n";
        break;
        if (playerChoice == 1)
            cout << "\nYou picked paper, and the computer picked paper. The game is tied!\n";
        break;
        if (playerChoice == 2)
            cout << "\nYou picked scissors, and the computer picked paper. You win!\n";
        break;


    case 2:

        if (playerChoice == 0)
            cout << "\nYou picked rock, and the computer picked scissors. You win!\n";
        break;
        if (playerChoice == 1)
            cout << "\nYou picked paper, and the computer picked scissors. You lose!\n";
        break;
        if (playerChoice == 2)
            cout << "\nYou picked scissors, and the computer picked scissors. The game is tied!\n";
        break;

    default:

        cout << "Invalid number, try again" << endl;

    }
    return 0;
}
Last edited on
First of all take into account that the computer choice can not be equal to 0 because the smallest value that it can have is 1

int computerChoice(){

srand(time(0));
int compChoice = 1 + (rand()%3);

return compChoice;

}

Secondly inclose in braces compound statements after if. For example instead of

1
2
3
4
5
6
7
8
9
        if (playerChoice == 0)
            cout << "\nYou picked rock, and the computer picked rock. The game is tied!\n";
        break;
        if (playerChoice == 1)
            cout << "\nYou picked paper, and the computer picked rock. You win!\n";
        break;
        if (playerChoice == 2)
            cout << "\nYou picked scissors, and the computer picked rock. You lose!\n";
        break;



write

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
        if (playerChoice == 0)
        {
            cout << "\nYou picked rock, and the computer picked rock. The game is tied!\n";
            break;
        }
        if (playerChoice == 1)
        {
            cout << "\nYou picked paper, and the computer picked rock. You win!\n";
            break;
        }
        if (playerChoice == 2)
        {
            cout << "\nYou picked scissors, and the computer picked rock. You lose!\n";
            break;
        }
        break;


Or you can write simply

1
2
3
4
5
6
7
        if (playerChoice == 0)
            cout << "\nYou picked rock, and the computer picked rock. The game is tied!\n";
        if (playerChoice == 1)
            cout << "\nYou picked paper, and the computer picked rock. You win!\n";
        if (playerChoice == 2)
            cout << "\nYou picked scissors, and the computer picked rock. You lose!\n";
        break;

Last edited on
Well...

1. You've got too many breaks. The switch statement cases give up at the first break; everything after it till the next case is "unreachable code". So you might as well as written

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
    switch(compNumber){

    case 0:

        if (playerChoice == 0)
            cout << "\nYou picked rock, and the computer picked rock. The game is tied!\n";
        break;

    case 1:

        if (playerChoice == 0)
            cout << "\nYou picked rock, and the computer picked paper. You lose!\n";
        break;

    case 2:

        if (playerChoice == 0)
            cout << "\nYou picked rock, and the computer picked scissors. You win!\n";
        break;

    default:

        cout << "Invalid number, try again" << endl;

    }


2. Your random number generator is going to return the numbers 1, 2, or 3 -- as you're adding on 1 -- while your switch is using 0, 1, and 2

3. You should call srand just the once, at program startup.

4. "else if" might be of use in your code?

5. A nd you're doing far too much typing. With a bit of thought you could cut your code right, right down (you know clock arithmetic, yes?)

Andy

PS For readbility reasons, you should keep the line length of your code down below, say. 100.
Last edited on
closed account (28poGNh0)
Your breaks are not within the if statement

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
# include <ctime>
# include <cstdlib>
using namespace std;

int computerChoice()
{
    srand(time(0));
    int compChoice = rand()2;

    return compChoice;
}

int main()
{
    int playerChoice = 0;

    cout << "Hello, and welcome to Rock, Paper, Scissors!\n\nRules of the game are: \n\n1) Rock beats scissors\n2) Scissors beats paper\n3) Paper beats rock.\n";
    cout <<"\nPlease make your choice: \n\nPick 0 for rock, 1 for paper and 2 for scissors (Press -1 to exit)." << endl;
    cin >> playerChoice;

    int compNumber = computerChoice();

    switch(compNumber)
    {
        case 0:
            if (playerChoice == 0)
            {
                cout << "\nYou picked rock, and the computer picked rock. The game is tied!\n";
                break;
            }
            if (playerChoice == 1)
            {
                cout << "\nYou picked paper, and the computer picked rock. You win!\n";
                break;
            }
            if (playerChoice == 2)
            {
                cout << "\nYou picked scissors, and the computer picked rock. You lose!\n";
                break;
            }
        case 1:
            if (playerChoice == 0)
            {
                cout << "\nYou picked rock, and the computer picked paper. You lose!\n";
                break;
            }
            if (playerChoice == 1)
            {
                cout << "\nYou picked paper, and the computer picked paper. The game is tied!\n";
                break;
            }
            if (playerChoice == 2)
            {
                cout << "\nYou picked scissors, and the computer picked paper. You win!\n";
                break;
            }
        case 2:
            if (playerChoice == 0)
            {
                cout << "\nYou picked rock, and the computer picked scissors. You win!\n";
                break;
            }
            if (playerChoice == 1)
            {
                cout << "\nYou picked paper, and the computer picked scissors. You lose!\n";
                break;
            }
            if (playerChoice == 2)
            {
                cout << "\nYou picked scissors, and the computer picked scissors. The game is tied!\n";
                break;
            }
        default:
            cout << "Invalid number, try again" << endl;
    }
    return 0;
}

Last edited on
I do concur that

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
            if (playerChoice == 0)
            {
                cout << "\nYou picked rock, and the computer picked rock. The game is tied!\n";
                break;
            }
            if (playerChoice == 1)
            {
                cout << "\nYou picked paper, and the computer picked rock. You win!\n";
                break;
            }
            if (playerChoice == 2)
            {
                cout << "\nYou picked scissors, and the computer picked rock. You lose!\n";
                break;
            }


woud be better re-coded as

1
2
3
4
5
6
7
            if (playerChoice == 0)
                cout << "\nYou picked rock, and the computer picked rock. The game is tied!\n";
            else if (playerChoice == 1)
                cout << "\nYou picked paper, and the computer picked rock. You win!\n";
            else if (playerChoice == 2)
                cout << "\nYou picked scissors, and the computer picked rock. You lose!\n";
            break;


Andy
Thanks guys...I actually initially only put the "break" at the end of each case, but for some reason, when things weren't working, I looked at an example online that did it with the breaks after each "if" statement. Should have known better. Same with the random number generator.

Thanks for the help.
Topic archived. No new replies allowed.