Having trouble with rock, paper, scissors problem

Hello everyone, I am having some trouble with my rock, paper, scissors application that I need to write for class, and I would like to know what I am doing wrong. It needs to have functions, and it needs to include switch statements. The menu choices also have to be characters instead of ints. The program compiles, but it doesn't run properly. Whenever I select one of the menu choices, the result always shows as a tie. Also, I have a do-while loop set up for my main function, but the no matter what choice is chosen, the choice is always invalid regardless of whether the choice is actually valid or not. For example, if I choose R for rock, the program asks me to select a valid choice, but still calls the game function and shows it as a tie. Any other kind of feedback is greatly welcomed and appreciated.

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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
  // Write a program that lets the user play a game of Rock, Paper, Scissors against the computer.
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <conio.h>
using namespace std;

//Show menu choices
void showMenu()
{
	cout << "R - Rock" << endl;
	cout << "P - Paper" << endl;
	cout << "S - Scissors" << endl;
	cout << "E - Exit" << endl;
}

//Get computer's choice
char getComp()
{
	//Default value
	char compChoice = 'X';

	int choice = rand() % 3;

	if (choice == 0)
		compChoice = 'R';
	else if (choice == 1)
		compChoice = 'P';
	else if (choice == 2)
		compChoice = 'S';

	return compChoice;
}

//Get user's choice
char getUser()
{
	//Default choice
	char userChoice = 'X';
	cout << "Please select your choice: " << endl;
	cin >> userChoice;

	return userChoice;
}

//Generate decision
void game(char user, char comp)
{
	if (user == comp)
		cout << "Tie!" << endl;
	else if (user == 'R' || 'r' && comp == 'P' || 'p')
		cout << "Paper beats rock, you lose!" << endl;
	else if (user == 'R' || 'r' && comp == 'S' || 's')
		cout << "Rock beats scissors, you win!";
	else if (user == 'P' || 'p' && comp == 'S' || 's')
		cout << "Scissors beats paper, you lose!" << endl;
	else if (user == 'P' || 'p' && comp == 'R' || 'r')
		cout << "Paper beats rock, you win!" << endl;
	else if (user == 'S' || 's' && comp == 'R' || 'r')
		cout << "Rock beats scissors, you lose!";
	else if (user == 'S' || 's' && comp == 'P' || 'p')
		cout << "Scissors beats paper, you win!";
}
int main()
{
	//Seed random number generator
	unsigned seed = time(0);

	         srand(seed);

	//Declare variables, default values
    char     user = 'X',
		     comp = 'X',
			 playAgain = 'X';

	//Loop of program
	do
	{
		//Show menu choice
		showMenu();

		//Get computer's choice
		getComp();

		//Get user's choice
		getUser();

		if (user == 'Q' || user == 'q')
			exit(0);

		//Input validation
		 if (user != 'R' || user != 'r', user != 'P' || user != 'p', user != 'S' || user != 's', user != 'Q' || user != 'q')
			cout << endl;
			cout << "Please select a valid choice." << endl;

		//Display choices
			switch (user)
			{
			case 'R': case 'r':
				cout << "You chose rock" << endl;
			case 'P': case 'p':
				cout << "You chose paper" << endl;
			case 'S': case 's':
				cout << "You chose scissors" << endl;
			}

			switch (comp)
			{
			case 'R':
				cout << "Computer chose rock" << endl;
			case 'P':
				cout << "Computer chose paper" << endl;
			case 'S':
				cout << "Computer chose scissors" << endl;
			}

		//Determine winner
		game(user, comp);

		//Play again?
		cout << "Do you want to play again? Choose 'Y' for yes, and 'N' for no" << endl;
		cin >> playAgain;
		if (playAgain != 'Y' || playAgain != 'y', playAgain != 'N' || playAgain != 'n')
			cout << "Invalid choice, please choose a valid choice" << endl;
	}
	while (playAgain == 'Y' || playAgain == 'y');
	return 0;
}
Your program has few logical mistakes. I have repaired and marked them in comments. Comment further, if you don't understand any part of it.

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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
 // Write a program that lets the user play a game of Rock, Paper, Scissors against the computer.
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <conio.h>
using namespace std;

//Show menu choices
void showMenu()
{
	cout << "R - Rock" << endl;
	cout << "P - Paper" << endl;
	cout << "S - Scissors" << endl;
	cout << "E - Exit" << endl;
}

//Get computer's choice
char getComp()
{
	//Default value
	char compChoice = 'X';

	int choice = rand() % 3;

	if (choice == 0)
		compChoice = 'R';
	else if (choice == 1)
		compChoice = 'P';
	else if (choice == 2)
		compChoice = 'S';

	return compChoice;
}

//Get user's choice
char getUser()
{
	//Default choice
	char userChoice = 'X';
	cout << "Please select your choice: " << endl;
	cin >> userChoice;

	return userChoice;
}

//Generate decision
void game(char user, char comp)
{
	if (user == comp||user==comp-32||user==comp+32)//added and subtracted 32 to remove case sensitivity
		cout << "Tie!" << endl;
	else if ((user == 'R' ||user== 'r') &&( comp == 'P' || comp=='p'))
		cout << "Paper beats rock, you lose!" << endl;
	else if ((user == 'R' ||user== 'r') && (comp == 'S' || comp=='s'))
		cout << "Rock beats scissors, you win!\n";
	else if ((user == 'P' ||user== 'p') &&( comp == 'S' || comp=='s'))
		cout << "Scissors beats paper, you lose!" << endl;
	else if ((user == 'P' || user=='p') && (comp == 'R' || comp=='r'))
		cout << "Paper beats rock, you win!" << endl;
	else if ((user == 'S' || user=='s') && (comp == 'R' || comp=='r'))
		cout << "Rock beats scissors, you lose!\n";
	else if ((user == 'S' || user=='s') &&( comp == 'P' || comp=='p'))
		cout << "Scissors beats paper, you win!\n";

}
int main()
{
	//Seed random number generator
	unsigned seed = time(0);

	         srand(seed);

	//Declare variables, default values
    char     user = 'X',
		     comp = 'X',
			 playAgain = 'X';

	//Loop of program
	do
	{
		//Show menu choice
		showMenu();

		//Get computer's choice
		comp=getComp();//Earlier you were not storing your returned value in a variable. Which leaded you to your main problem.

		//Get user's choice
		user=getUser();//Earlier you were not storing your returned value in a variable. Which leaded you to your main problem.

		if (user == 'E' || user == 'e')//Its 'E' not 'Q'
			exit(0);

		//Input validation
		/* if ((user != 'R' || user != 'r')&& (user != 'P' || user != 'p')&&( user != 'S' || user != 's')&&( user != 'Q' || user != 'q')){
			cout << endl;
			cout << "Please select a valid choice." << endl;}*/

		//Display choices
			switch (user)
			{
			case 'R': case 'r':
				cout << "You chose rock" << endl;
				break;//your program also lacked brake statements.
			case 'P': case 'p':
				cout << "You chose paper" << endl;
				break;
			case 'S': case 's':
				cout << "You chose scissors" << endl;
				break;
            default:
                cout<<"\nPlease select a valid choice.\n";//The whole if statement can be replaced by a default statement.
			}

			switch (comp)
			{
			case 'R':
				cout << "Computer chose rock" << endl;
				break;
			case 'P':
				cout << "Computer chose paper" << endl;
				break;
			case 'S':
				cout << "Computer chose scissors" << endl;
                break;
			}

		//Determine winner
		game(user, comp);

		//Play again?
		cout << "Do you want to play again? Choose 'Y' for yes, and 'N' for no" << endl;
		cin >> playAgain;
		if (playAgain != 'Y' &&playAgain != 'y'&& playAgain != 'N' && playAgain != 'n'){//Earlier if statement was logically wrong
			cout << "Invalid choice, please choose a valid choice" << endl;
		}
	}
	while (playAgain == 'Y' || playAgain == 'y');
	return 0;
}
Last edited on
Thanks for the reply hiphop12ism, the fixes you made allowed my program to run. There were some simple mistakes I should have caught like the breaks in my switch statement, and the variables not getting retrieved. One question I have is about the -32 +32 in line 49. Is it because the user choices are not case sensitive, but the computer choices are? So for example, if user = r, and comp = R, would it show as an invalid choice? Or is it for some other reason?

edit: The program also displays the computer's choice, even when the user's input is invalid. I've tried to put an if statement in the computer's choice, but it wouldn't even display the computer's choice, and it won't display the decision at all.
Last edited on
Yes, you are right about the case sensitivity part.
I believe that you don't want to show the computer]s choice when the user enters an invalid choice.
For that, you could give a flag variable to solve your problem with invalid choice.

For eg:
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
int flag=0;//here is the variable which will do the job.
			switch (user)
			{
			case 'R': case 'r':
				cout << "You chose rock" << endl;
				break;//your program also lacked brake statements.
			case 'P': case 'p':
				cout << "You chose paper" << endl;
				break;
			case 'S': case 's':
				cout << "You chose scissors" << endl;
				break;
            default:
                cout<<"\nPlease select a valid choice.\n";//The whole if statement can be replaced by a default statement.
                flag=1;
			}
            if(!flag){
			switch (comp)
			{
			case 'R':
				cout << "Computer chose rock" << endl;
				break;
			case 'P':
				cout << "Computer chose paper" << endl;
				break;
			case 'S':
				cout << "Computer chose scissors" << endl;
                break;
			}

		//Determine winner
		game(user, comp);
            }
Last edited on
Topic archived. No new replies allowed.