Rock Paper Scissors Cases not working

Hello, I am very new to C++, this is literally the second program I have tried to make after the basic text output and playing around with variables. I am trying to make a simple rock paper scissors game that works by first choosing if you want to play a game (y/n) and then two people input (r/p/s) and it outputs the winner/or if it is a draw. However my Case Switch statements always output as yes and then the default "neither of you has chosen an object". I'm expecting there to probably be alot wrong with the code, so any help would be appreciated. Here's the code:

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
   int main()
{   
    char answer1;
    char answer2;
    char answer3;
    int outcomes;
    int choice;
    
    cout << "Rock Paper Scissors\n\n";
    cout << "Play a game? y/n\n";;
    getch()==answer1;
    switch(choice) {
    default:(answer1=!110||121);
    system("pause");
    case 1:(answer1=110);
    cout << "OK then...\n";
    system("pause");
    case 2:(answer1=121);
    {cout << "\nPlayer 1, Choose either rock(r), paper(p) or scissors(s).\n";
    getch()==answer2;
    cout << "Player 2, Choose either rock(r), paper(p) or scissors(s).\n";
    getch()==answer3;
    //r=114
    //p=112   
    //s=115
          switch(outcomes) {
           case 1:(answer2==114&&answer3==115);
           {cout << "Player 1 Wins.\n";
           system("pause");}
           case 2:(answer2==115&&answer3==112);
           {cout << "Player 1 Wins.\n";
           system("pause");} 
           case 3:(answer2==112&&answer3==114);
           {cout << "Player 1 Wins.\n";
           system("pause");} 
           case 4:(answer3==114&&answer2==115);
           {cout << "Player 2 Wins.\n";
           system("pause");}
           case 5:(answer3==115&&answer2==112);
           {cout << "Player 2 Wins.\n";
           system("pause");} 
           case 6:(answer3==112&&answer2==114);
           {cout << "Player 2 Wins.\n";
           system("pause");} 
           case 7:(answer2==114&&answer3==114);
           {cout << "It's a Draw.\n";
           system("pause");} 
           case 8:(answer2==115&&answer3==115);
           {cout << "It's a Draw.\n";
           system("pause");} 
           case 9:(answer2==112&&answer3==112);
           {cout << "It's a Draw.\n";
           system("pause");}
           default:((answer2=!112,114,115)||(answer3=!112,114,115));
           {cout << "One of you hasn't entered rock(r), paper(p) or scissors(s).\n";
           system("pause");}}}
 }} 

Your first switch statement depends on the variable choice .You never put any value into choice so it's just some garbage value.

Your second switch statement depends on the variable outcomes .You never put any value into outcomes so it's just some garbage value.

There is a lot more wrong, but fix this first.
Several things:

1) switch does not take conditionals. The case value IS the conditional. E.g., the following control structures are equivalent:
1
2
3
4
5
6
7
switch(number) {
case 5: ... // Code
} 

if(number == 5) {
... // Code
}

In your case, that means the switches are working on an uninitialized variable. If it would run (I doubt it does), it would go directly to "default", because uninitialized numbers generally (in my compiler anyway) have a very large negative value.

2) "getch()==answer1;" doesn't make sense. Firstly, the variable being assigned a value is ALWAYS on the left. Secondly, "==" is the comparator, not the assignment operator. Thirdly, getch() doesn't work that way. Fourthly, getch() is depracated. Use std::cin instead (<iostream>).

3) Don't use system("pause"). It kills unborn babies. Or is unsafe or something, who knows. Anyway, don't use it. I think you meant to use "break" (which is used to "stop" a switch case. Without break, case 1 will continue until the end, executing EACH case below it).

4) Don't put }'s on the same line. It's difficult to read (and count)!

5) Your main must return a value. Add "return 0" at the end.
Last edited on
Yep, the switch is the big issue here. I've added some other comments to help you learn.

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
int main()
{   
    char answer1;
    char answer2;
    char answer3;
    int outcomes;
    int choice;
    
    cout << "Rock Paper Scissors\n\n";
    cout << "Play a game? y/n\n";; // Two ;'s not needed here
    getch()==answer1; // You have a conditional here that doesn't work, remove it
    cin >> answer1; // This is an easier way of doing getch();
    switch(choice) { // You didnt set 'choice' before/
    default:(answer1=!110||121); // This type of statement doesn't work, use (answer1!=110 || answer1 !=121)
    system("pause"); // Keep doing this and someone will scream. Never use system(anything)!
    case 1:(answer1=110); // YOu are setting answer1 to 100, I assume you want to compare with (answer1==110)
    cout << "OK then...\n";
    system("pause");
    case 2:(answer1=121); // Unless choice is 1, you won't see this code, also (answer1=121) just sets answer1, it doesn't really do anything else.
    {cout << "\nPlayer 1, Choose either rock(r), paper(p) or scissors(s).\n";//Why the {?
    getch()==answer2;
    cout << "Player 2, Choose either rock(r), paper(p) or scissors(s).\n";
    getch()==answer3;
    //r=114
    //p=112   
    //s=115
          switch(outcomes) {
           case 1:(answer2==114&&answer3==115);// Try using "r" instead of 114, it makes code easier to read.
           {cout << "Player 1 Wins.\n";
           system("pause");}
           case 2:(answer2==115&&answer3==112);
           {cout << "Player 1 Wins.\n";
           system("pause");} 
           case 3:(answer2==112&&answer3==114);
           {cout << "Player 1 Wins.\n";
           system("pause");} 
           case 4:(answer3==114&&answer2==115);
           {cout << "Player 2 Wins.\n";
           system("pause");}
           case 5:(answer3==115&&answer2==112);
           {cout << "Player 2 Wins.\n";
           system("pause");} 
           case 6:(answer3==112&&answer2==114);
           {cout << "Player 2 Wins.\n";
           system("pause");} 
           case 7:(answer2==114&&answer3==114);
           {cout << "It's a Draw.\n";
           system("pause");} 
           case 8:(answer2==115&&answer3==115);
           {cout << "It's a Draw.\n";
           system("pause");} 
           case 9:(answer2==112&&answer3==112);
           {cout << "It's a Draw.\n";
           system("pause");}
           default:((answer2=!112,114,115)||(answer3=!112,114,115));
           {cout << "One of you hasn't entered rock(r), paper(p) or scissors(s).\n";
           system("pause");}}}//You didn't need this many }s, try to get rid of some opening ones too.
 }} 
Last edited on
Something like this would be cleaner:
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
#include <iostream>
using namespace std;

int main()
{
	char play;
	char answer1;
	char answer2;
	cout << "Rock Paper Scissors" << endl << endl;
	cout << "Play a game? y/n" << endl;
	cin >> play;
	while (play == "y")
	{
		cout << "OK then..." << endl << endl;
		cout << "Player 1, Choose either rock(r), paper(p) or scissors(s)." <<endl;
		cin  >> answer1;
		cout << "Player 2, Choose either rock(r), paper(p) or scissors(s)." << endl;
		cin  >> answer2;
		
		if (answer1 != "r" && answer1 != "p" && answer1 != "s" || 
                    answer2 != "r" && answer2 != "p" && answer2 != "s")
		{
			cout << "One of you hasn't entered rock(r), paper(p) or scissors(s)." << endl;
		}
		else if (answer1 == "r" && answer2 == "s" ||
			 answer1 == "s" && answer2 == "p" ||
			 answer1 == "p" && answer2 == "r" )
		{
			cout << "Player 1 Wins." << endl;
		}
		else if (answer1 == "p" && answer2 == "s" ||
			 answer1 == "r" && answer2 == "p" ||
		         answer1 == "s" && answer2 == "r" )
		{
			cout << "Player 2 Wins." << endl;
		}
		else
		{
			cout << "It's a Draw." << endl;
		}
		
		cout << "Play again? y/n" << endl;
		cin  >> play;
	}
	return 0;
}
Last edited on
Thanks guys, I reckon I can fix it now :D
Topic archived. No new replies allowed.