Rock, Paper, Scissors program issue...

I can't seem to make this work. For some reason the PlayerChoice is getting randomized so that the users input doesn't matter as the program will just randomly assign a value to PlayerChoice. As far as I can tell the CompChoice should be randomized, I can't see why the PlayerChoice is being randomized, someone help please?

Thanks.

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

int main()
{
	//Determine computers choices
	int PlayerChoice;
	srand(time(0));
	int CompChoice = rand() % 2;

	cout << "Enter 0(Rock), 1(Paper), or 2(Scissors) > ";
	cin >> PlayerChoice;

	switch(CompChoice)
	{
		case 0:
			if (PlayerChoice == 0);
			cout << "You played Rock." << endl;
			cout << "The computer played Rock." << endl;
			cout << "Game is a Tied." << endl;
			cout << "You beat the computer!" << endl;
			cout << "You beat the computer!" << endl;
			break;

			if (PlayerChoice == 1);
			cout << "You played Rock." << endl;
			cout << "The computer played Paper." << endl;
			cout << "Rats! The computer won." << endl;
			cout << "Rats! The computer won." << endl;
			break;

			if (PlayerChoice == 2);
			cout << "You played Rock." << endl;
			cout << "The computer played Scissors." << endl;
			cout << "You beat the computer!" << endl;
			cout << "You beat the computer!" << endl;
			break;

		case 1:
			if (PlayerChoice == 0);
			cout << "You played Paper." << endl;
			cout << "The computer played Rock." << endl;
			cout << "You beat the computer!" << endl;
			cout << "You beat the computer!" << endl;
			break;

			if (PlayerChoice == 1);
			cout << "You played Paper." << endl;
			cout << "The computer played Paper." << endl;
			cout << "Game is a Tied." << endl;
			cout << "You beat the computer!" << endl;
			cout << "You beat the computer!" << endl;
			break;

			if (PlayerChoice == 2);
			cout << "You played Paper." << endl;
			cout << "The computer played Scissors." << endl;
			cout << "Rats! The computer won." << endl;
			cout << "Rats! The computer won." << endl;
			break;

		case 2:
			if (PlayerChoice == 0);
			cout << "You played Scissors." << endl;
			cout << "The computer played Rock." << endl;
			cout << "Rats! The computer won." << endl;
			cout << "Rats! The computer won." << endl;
			break;

			if (PlayerChoice == 1);
			cout << "You played Scissors." << endl;
			cout << "The computer played Paper." << endl;
			cout << "You beat the computer!" << endl;
			cout << "You beat the computer!" << endl;
			break;

			if (PlayerChoice == 2);
			cout << "You played Scissors." << endl;
			cout << "The computer played Scissors." << endl;
			cout << "Game is a Tied." << endl;
			cout << "You beat the computer!" << endl;
			cout << "You beat the computer!" << endl;
			break;

		default: cout << "Invalid number, Play again" << endl;
	}

	return 0;
}
if statements execute the next {code block} or up until the next semicolon

1
2
3
4
5
if(foo)
{
  PartOfIf();
}
NotPartOfIf();


1
2
3
if(foo)
  PartOfIf();
NotPartOfIf();


1
2
3
// here, since the semicolon is immediately after the if, the if is basically "empty"
if(foo);
NotPartOfIf();


This is the problem with your code. All your if statements are empty.

You need to get rid of that semicolon and put {braces} around what you want to be part of the if.
@Vyperhate
Also, your rand number generator int CompChoice = rand() % 2;, returns a 0 or a 1, never a two. You'll have to change the two to a three, to get all three numbers
Last edited on
Ok thanks for your help. Now it doesn't seem to rand the PlayerChoice however the only player input that works now is "0"... Any other input just ends the program (does not activate the default in the switch) Ideas?

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
98
99
100
101
102
103
104
105
106
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

int main()
{
	//Determine computers choices
	int PlayerChoice;
	srand(time(0));
	int CompChoice = rand() % 3;

	cout << "Enter 0(Rock), 1(Paper), or 2(Scissors) > ";
	cin >> PlayerChoice;

	switch(CompChoice)
	{
		case 0:
			if (PlayerChoice == 0)
			{
			cout << "You played Rock." << endl;
			cout << "The computer played Rock." << endl;
			cout << "You beat the computer!" << endl;
			cout << "You beat the computer!" << endl;
			}
			break;

			if (PlayerChoice == 1)
			{
			cout << "You played Paper." << endl;
			cout << "The computer played Rock." << endl;
			cout << "You beat the computer!" << endl;
			cout << "You beat the computer!" << endl;
			}
			break;

			if (PlayerChoice == 2)
			{
			cout << "You played Scissors." << endl;
			cout << "The computer played Rock." << endl;
			cout << "Rats! The computer won." << endl;
			cout << "Rats! The computer won." << endl;
			}
			break;

		case 1:
			if (PlayerChoice == 0)
			{
			cout << "You played Rock." << endl;
			cout << "The computer played Paper." << endl;
			cout << "Rats! The computer won." << endl;
			cout << "Rats! The computer won." << endl;
			}
			break;

			if (PlayerChoice == 1)
			{
			cout << "You played Paper." << endl;
			cout << "The computer played Paper." << endl;
			cout << "You beat the computer!" << endl;
			cout << "You beat the computer!" << endl;
			}
			break;

			if (PlayerChoice == 2)
			{
			cout << "You played Scissors." << endl;
			cout << "The computer played Paper." << endl;
			cout << "You beat the computer!" << endl;
			cout << "You beat the computer!" << endl;
			}
			break;

		case 2:
			if (PlayerChoice == 0)
			{
			cout << "You played Rock." << endl;
			cout << "The computer played Scissors." << endl;
			cout << "You beat the computer!" << endl;
			cout << "You beat the computer!" << endl;
			}
			break;

			if (PlayerChoice == 1)
			{
			cout << "You played Paper." << endl;
			cout << "The computer played Scissors." << endl;
			cout << "Rats! The computer won." << endl;
			cout << "Rats! The computer won." << endl;
			}
			break;

			if (PlayerChoice == 2)
			{
			cout << "You played Scissors." << endl;
			cout << "The computer played Scissors." << endl;
			cout << "You beat the computer!" << endl;
			cout << "You beat the computer!" << endl;
			}
			break;

		default: cout << "Invalid entry. Game Terminated." << endl;
	}

	return 0;
}
if you break;, you exit the switch.
Topic archived. No new replies allowed.