C++ Rock, Paper, Scissors Function Woes

Pages: 12
Hey guys, so I'm having a bit of trouble with my homework and was hoping someone here would be able to help me. My win, lose, tie functions are giving me a bit of trouble and I'm having a hard time figuring out what is wrong. No matter what I do it will print out that I win, lose, and tie, which of course is not correct. These are the teacher's instructions for the assignment.

INSTRUCTIONS:
If the user selects 'p':

1. First the program should call a function named getComputerChoice to get the computer's choice in the game. The getComputerChoice function should generate a random number between 1 and 3. If the random number is 1 the computer has chosen Rock, if the random number is 2 the user has chosen Paper, and if the random number is 3 the computer has chosen Scissors. (Hint: return an int from this function and make the function have no arguments). This function is REQUIRED to be a value returning function (do NOT make a void function).



2. Next the program should call a function named getPlayerChoice to get the user's (player's) choice in the game. Have the user's choice be represented by a number just like the computer's was. The getPlayerChoice function should display a menu similar to the one below and then get a 1, 2, or 3 as the user's choice for Rock, Paper or Scissors. Do not allow the user to enter an invalid choice - they must enter 1, 2, or 3 (use integers for the menu on this instead of chars). (Hint: return an int from this function and make the function have no arguments). This function is REQUIRED to be a value returning function (do NOT make a void function).
Rock, Paper or Scissors?

1) Rock

2) Paper

3) Scissors

Please enter your choice:



3. Next the program should display what the user chose and what the computer chose. You will need to convert both the user's and the computer's integer choices of 1, 2, or 3 to the corresponding strings: Rock, Paper, or Scissors. The display should be similar to the following (in the example below the user chose 3 for Scissors and the computer chose 2 for Paper)
You chose: Scissors

The computer chose: Paper



4. Next the program should call a function named isTie (Hint: have the function return a bool and take two arguments) to see if the game was a tie. This function is REQUIRED to be a value returning function and must take TWO arguments (do NOT make a void function). The game is a tie if both the computer and user made the same choice. If the game was a tie display the appropriate message. An example of a tie is shown below with the appropriate message on the third line.

You choose: Scissors

The computer chose: Scissors

It's a TIE!



5. If the game was NOT a tie, the program should next determine if the player (user) has won the game. You should use a function called isPlayerWinnerto do this, the function should take two arguments (the player choice and the computer choice and return true if the player is a winner and false if the player is not a winner). This function is REQUIRED to be a value returning function (do NOT make a void function) The function is required to take two arguments.

After calling the function, based on the result of it, if the player has won the program should display a message similar to the one below telling the user they won. In the example below the user's winning choice is Rock and the computer's losing choice is Scissors. (Remember the rules for winning and losing are at the top of the page)

You choose: Rock

The computer chose: Scissors

You WIN!



6. If the game was NOT a tie and the user (player) did NOT win this means the computer won and the program should display a message saying the user has lost. In the example below the computer's winning choice is Paper (the user's losing choice is Rock). Do not make this complicated, if it was not a tie and the player did not win the computer won.

You choose: Rock

The computer chose: Paper

Sorry you LOSE.



If the user selects q: Quit the program

If the user selects anything other than p or q: Display an error message.

The program should keep re-displaying the menu and let the user play as many games against the computer as they would like.

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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;

int getComputerChoice(); /* FUNCTION PROTOTYPES ARE LISTED HERE */
int getPlayerChoice();
bool isTie(int, int);
bool isPlayerWinner(int, int); /* END OF FUNCTION PROTOTYPES */

int main()
{
	char userChoice; /* VARIABLES ARE DEFINED HERE*/
	int playerChoice = 0;
	int computerChoice = 0;
	int myChoice = 0;
	int randomCompNum = 0;
	bool hasWon; /* END OF VARIABLE DEFINING */

	do
	{
		cout << "\n"
			<< "ROCK PAPER SCISSORS MENU\n"
			<< "------------------------\n"
			<< "p) Play Game\n"
			<< "q) Quit\n"
			<< "Please enter your choice : \n\n";
		cin >> userChoice;

		if (userChoice == 'p')
		{
			playerChoice = getPlayerChoice();
			computerChoice = getComputerChoice();

			hasWon = (isPlayerWinner(myChoice, randomCompNum) == true);
			{
				cout << "You WIN!\n";
			}

			hasWon = (isPlayerWinner(myChoice, randomCompNum) == false);
			{
				cout << "Sorry you LOSE.\n";
			}

			if (isTie(myChoice, randomCompNum) == true)
			{
				cout << "It's a TIE!\n\n\n";
			}

			else
			{
				return false;
				cout << "\n";
			}
		}

		else if (userChoice == 'q')
		{
			cout << "You have chosen to quit the program. Thank you for using the program!\n";
		}

		else if (userChoice != 'q' || userChoice != 'p')
		{
			cout << "Invalid selection. Try again.\n\n";
		}
	}

	while (userChoice != 'q');

	system("PAUSE");
	return 0;
}

int getComputerChoice()
{

	srand((unsigned int)time(NULL));
	int randomCompNum = rand() % 3 + 1;

	if (randomCompNum == 1)
	{
		cout << "The computer chose : Rock\n";
	}

	else if (randomCompNum == 2)
	{
		cout << "The computer chose : Paper\n";
	}

	else if (randomCompNum == 3)
	{
		cout << "The computer chose : Scissors\n";
	}

	return randomCompNum;
}

int getPlayerChoice()
{
	int myChoice;

	cout << "\n\nRock, Paper, or Scissors?\n"
		<< "1) Rock\n"
		<< "2) Paper\n"
		<< "3) Scissors\n"
		<< "Please enter your choice : \n";
	cin >> myChoice;

	if (myChoice == 1)
	{
		cout << "You chose : Rock\n";
	}

	else if (myChoice == 2)
	{
		cout << "You chose : Paper\n";
	}

	else if (myChoice == 3)
	{
		cout << "You chose : Scissors\n";
	}

	return myChoice;

	while (myChoice < 1 || myChoice > 3)
	{
		cout << "Please pick a number between 1 & 3.\n";
		cin >> myChoice;
	}

}

bool isPlayerWinner(int myChoice, int randomCompNum)
{
	if ((myChoice == 1) && (randomCompNum == 3))
	{
		return true; 
	}

	else if ((myChoice == 3) && (randomCompNum == 2))
	{
		return true;
	}

	else if ((myChoice == 2) && (randomCompNum == 1))
	{
		return true;
	}

	else if ((randomCompNum == 3) && (myChoice == 1))
	{
		return false;
	}

	else if ((randomCompNum == 3) && (myChoice == 2))
	{
		return false;
	}

	else if ((randomCompNum == 2) && (myChoice == 1))
	{
		return false;
	}
}

bool isTie(int myChoice, int randomCompNum)
{
	if (myChoice == randomCompNum)
	{
		return true;
	}

	else if (myChoice != randomCompNum)
	{
		return false;
	}
}
Last edited on
One thing right off the bat is that the below expressions aren't conditional. hasWon will always get the value of whatever the result of the expression in parentheses evaluates to which is why you're always displaying "You WIN!" and "Sorry you LOSE." . Did you mean both of these to be if structures?

1
2
3
4
5
6
7
8
9
hasWon = (isPlayerWinner(myChoice, randomCompNum) == true);  //Should this be an if condition?
			{
				cout << "You WIN!\n";
			}

			hasWon = (isPlayerWinner(myChoice, randomCompNum) == false);  //Should this be an if conditoin?
			{
				cout << "Sorry you LOSE.\n";
			}
Additionally, and I think this explain why your tie outcome always displays, you initialize randomCompNum as 0 in the main function, and that variable never changes. You're always passing
a value of 0 (false) to your functions. I think you mean to pass computerChoice (from line 34) as the argument, not randcomCompNum

1
2
3
4
if (isTie(myChoice, randomCompNum) == true)  //randomCompNum still holds the value of 0 that you initialized it to; should this to be computerchoice instead?
			{
				cout << "It's a TIE!\n\n\n";
			}
Last edited on
Hey, mastakhan, thank you for your help! I'm going to work on the isTie function first and I do believe you are correct that computerChoice variable should be passed in instead of randomCompNum so I will fix that.

EDIT: I made some changes to my code and will share the updated code here for you to look at. I believe I have made progress, but now it won't output It's a TIE even when it is a tie.
Last edited on
Hi,

I am not a fan of a do loop like you have it. Note you have the end condition 3 times. Just because a do loop always executes once, is not a reason on it's own to use it. If you are going to use a do loop, put the while part on the same line as the closing brace, so it doesn't look like a while loop with a null statement.

You could have a while loop with a bool Quit variable as the condition.

The return on line 53 ends the entire program.

This is still weird:
if (hasWon = (isPlayerWinner(myChoice, computerChoice) == true))

You already have a function to detect a win, why double up the confusion?
Hi TheIdeasMan,

I made the change of having the while part on the same line as the closing brace. My apologies for that. I try to keep things extra organized, but I can see why that would throw people off as it's not really "proper code etiquette."

Good point on the hasWon bool. Honestly, I got stuck halfway through this program and found someone else' for the same homework online and used their structure. Looking at it now, I can totally see why the hasWon bool is basically pointless SINCE I have the bool function isPlayerWinner. Did I understand you correctly?

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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;

int getComputerChoice(); /* FUNCTION PROTOTYPES ARE LISTED HERE */
int getPlayerChoice();
bool isTie(int, int);
bool isPlayerWinner(int, int); /* END OF FUNCTION PROTOTYPES */

int main()
{
	char userChoice; /* VARIABLES ARE DEFINED HERE*/
	int playerChoice;
	int computerChoice;
	int myChoice = 0;
	int randomCompNum = 0; /* END OF VARIABLE DEFINING */

	do
	{
		cout << "\n"
			<< "ROCK PAPER SCISSORS MENU\n"
			<< "------------------------\n"
			<< "p) Play Game\n"
			<< "q) Quit\n"
			<< "Please enter your choice : \n";
		cin >> userChoice;

		if (userChoice == 'p')
		{
			playerChoice = getPlayerChoice();
			computerChoice = getComputerChoice();

			if (isPlayerWinner(playerChoice, computerChoice) == true)
			{
				cout << "You WIN!\n\n";
			}

			else if (isPlayerWinner(playerChoice, computerChoice) == false)
			{
				cout << "Sorry you LOSE.\n\n";
			}

		    else if (isTie(myChoice, computerChoice) == true)
			{
				cout << "It's a TIE!\n\n\n";
			}

		    else if (isTie(myChoice, computerChoice) == false)
			{
				cout << "\n";
			}
		}

		else if (userChoice == 'q')
		{
			cout << "You have chosen to quit the program. Thank you for using the program!\n";
		}

		else if (userChoice != 'q' || userChoice != 'p')
		{
			cout << "Invalid selection. Try again.\n\n";
		}
	}while (userChoice != 'q');

    //system("PAUSE");
	return 0;
}

int getComputerChoice()
{

	srand((unsigned int)time(NULL));
	int randomCompNum = rand() % 3 + 1;

	if (randomCompNum == 1)
	{
		cout << "The computer chose : Rock\n\n";
	}

	else if (randomCompNum == 2)
	{
		cout << "The computer chose : Paper\n\n";
	}

	else if (randomCompNum == 3)
	{
		cout << "The computer chose : Scissors\n\n";
	}

	return randomCompNum;
}

int getPlayerChoice()
{
	int myChoice;

	cout << "\n\nRock, Paper, or Scissors?\n"
		<< "1) Rock\n"
		<< "2) Paper\n"
		<< "3) Scissors\n"
		<< "Please enter your choice : \n";
	cin >> myChoice;

	if (myChoice == 1)
	{
		cout << "\nYou chose : Rock\n";
	}

	else if (myChoice == 2)
	{
		cout << "\nYou chose : Paper\n";
	}

	else if (myChoice == 3)
	{
		cout << "\nYou chose : Scissors\n";
	}

	return myChoice;

	while (myChoice < 1 || myChoice > 3)
	{
		cout << "Please pick a number between 1 & 3.\n";
		cin >> myChoice;
	}

}

bool isPlayerWinner(int myChoice, int randomCompNum)
{
	if ((myChoice == 1) && (randomCompNum == 3))
	{
		return true; 
	}

	else if ((myChoice == 3) && (randomCompNum == 2))
	{
		return true;
	}

	else if ((myChoice == 2) && (randomCompNum == 1))
	{
		return true;
	}

	else if ((randomCompNum == 3) && (myChoice == 1))
	{
		return false;
	}

	else if ((randomCompNum == 3) && (myChoice == 2))
	{
		return false;
	}

	else if ((randomCompNum == 2) && (myChoice == 1))
	{
		return false;
	}
}

bool isTie(int myChoice, int randomCompNum)
{
	if (myChoice == randomCompNum)
	{
		return true;
	}

	else if (myChoice != randomCompNum)
	{
		return false;
	}
}
Last edited on
Looking at it now, I can totally see why the hasWon bool is basically pointless SINCE I have the bool function isPlayerWinner. Did I understand you correctly?


Yes, but you haven't changed the code.

Honestly, I got stuck halfway through this program and found someone else' for the same homework online and used their structure.


So that is plagiarism, careful you don't get busted for that, deserve it if you do.

Last edited on
I have changed it though, have I not? I removed the hasWon bool entirely.

I wouldn't say I directly copied it line of code for line of code, but I used it to get ideas on how to make mine work (which isn't really the case as you can see.) My intentions were not to take someone else's work and blatantly copy it and submit it as my own.
Last edited on
I have changed it though, have I not? I removed the hasWon bool entirely.


Now you have edited it, yes.

The comparison to true is redundant:

if (isPlayerWinner(playerChoice, computerChoice) == true)

I'm sorry, I don't understand. Don't I need the true/false conditions? Bear with me please as I'm very new to C++. This is currently my updated code and no matter what I try it always prints it's a win if the game ties and it also prints the new line from else if for the isTie bool regardless as well.


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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;

int getComputerChoice(); /* FUNCTION PROTOTYPES ARE LISTED HERE */
int getPlayerChoice();
bool isTie(int, int);
bool isPlayerWinner(int, int); /* END OF FUNCTION PROTOTYPES */

int main()
{
	char userChoice; /* VARIABLES ARE DEFINED HERE*/
	int playerChoice;
	int computerChoice; /* END OF VARIABLE DEFINING */

	do
	{
		cout << "\n"
			<< "ROCK PAPER SCISSORS MENU\n"
			<< "------------------------\n"
			<< "p) Play Game\n"
			<< "q) Quit\n"
			<< "Please enter your choice : \n";
		cin >> userChoice;

		if (userChoice == 'p')
		{
			playerChoice = getPlayerChoice();
			computerChoice = getComputerChoice();

			if (isPlayerWinner(playerChoice, computerChoice))
			{
				cout << "You WIN!\n\n";
			}

			else if (isPlayerWinner(playerChoice, computerChoice) == false)
			{
				cout << "Sorry you LOSE.\n\n";
			}

			if (isTie(playerChoice, computerChoice))
			{
				cout << "It's a TIE!\n\n";
			}

			else if (isTie(playerChoice, computerChoice) == false)
			{
				cout << "\n";
			}
		}

		else if (userChoice == 'q')
		{
			cout << "You have chosen to quit the program. Thank you for using the program!\n";
		}

		else if (userChoice != 'q' || userChoice != 'p')
		{
			cout << "Invalid selection. Try again.\n\n";
		}
	}while (userChoice != 'q');

    //system("PAUSE");
	return 0;
}

int getComputerChoice()
{

	srand((unsigned int)time(NULL));
	int randomCompNum = rand() % 3 + 1;

	if (randomCompNum == 1)
	{
		cout << "The computer chose : Rock\n\n";
	}

	else if (randomCompNum == 2)
	{
		cout << "The computer chose : Paper\n\n";
	}

	else if (randomCompNum == 3)
	{
		cout << "The computer chose : Scissors\n\n";
	}

	return randomCompNum;
}

int getPlayerChoice()
{
	int myChoice;

	cout << "\n\nRock, Paper, or Scissors?\n"
		<< "1) Rock\n"
		<< "2) Paper\n"
		<< "3) Scissors\n"
		<< "Please enter your choice : \n";
	cin >> myChoice;

	if (myChoice == 1)
	{
		cout << "\nYou chose : Rock\n";
	}

	else if (myChoice == 2)
	{
		cout << "\nYou chose : Paper\n";
	}

	else if (myChoice == 3)
	{
		cout << "\nYou chose : Scissors\n";
	}

	return myChoice;

	while (myChoice < 1 || myChoice > 3)
	{
		cout << "Please pick a number between 1 & 3.\n";
		cin >> myChoice;
	}

}

bool isPlayerWinner(int myChoice, int randomCompNum)
{
	if ((myChoice == 1) && (randomCompNum == 3))
	{
		return true; 
	}

	else if ((myChoice == 3) && (randomCompNum == 2))
	{
		return true;
	}

	else if ((myChoice == 2) && (randomCompNum == 1))
	{
		return true;
	}

	else if ((randomCompNum == 3) && (myChoice == 1))
	{
		return false;
	}

	else if ((randomCompNum == 3) && (myChoice == 2))
	{
		return false;
	}

	else if ((randomCompNum == 2) && (myChoice == 1))
	{
		return false;
	}
}

bool isTie(int myChoice, int randomCompNum)
{
	if (myChoice == randomCompNum)
	{
		return true;
	}

	else if (myChoice != randomCompNum)
	{
		return false;
	}
}
Last edited on
whenever there is a conditional expression, it is evaluated implicitly to true or false. If the condition is a call to a function which returns a bool, the comparison to a bool is redundant. Try it.

The condition can be negated with !

1
2
3
4
5
6
7
8
9
if (isPlayerWinner(playerChoice, computerChoice))
			{
				cout << "You WIN!\n\n";
			}

			else if (! isPlayerWinner(playerChoice, computerChoice) == false)
			{
				cout << "Sorry you LOSE.\n\n";
			}



1
2
3
4
5
bool Quit = false;
while (!Quit) {


}


Think about what is happening, there are 3 situations: Win ; Draw, Lose. So reflect that with the else if 's
Okay, I see what you mean about the comparing to a bool being redundant and I've fixed that in my code, BUT I am still lost on why whenever I tie it's printing You WIN! instead of It's a TIE...

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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;

int getComputerChoice(); /* FUNCTION PROTOTYPES ARE LISTED HERE */
int getPlayerChoice();
bool isTie(int, int);
bool isPlayerWinner(int, int); /* END OF FUNCTION PROTOTYPES */

int main()
{
	char userChoice; /* VARIABLES ARE DEFINED HERE*/
	int playerChoice;
	int computerChoice; /* END OF VARIABLE DEFINING */

	do
	{
		cout << "\n"
			<< "ROCK PAPER SCISSORS MENU\n"
			<< "------------------------\n"
			<< "p) Play Game\n"
			<< "q) Quit\n"
			<< "Please enter your choice : \n";
		cin >> userChoice;

		if (userChoice == 'p')
		{
			playerChoice = getPlayerChoice();
			computerChoice = getComputerChoice();

			if (isPlayerWinner(playerChoice, computerChoice))
			{
				cout << "You WIN!\n\n";
			}

			else if (! isPlayerWinner(playerChoice, computerChoice))
			{
				cout << "Sorry you LOSE.\n\n";
			}

			else if (isTie(playerChoice, computerChoice))
			{
				cout << "It's a TIE!\n\n";
			}

			else if (! isTie(playerChoice, computerChoice))
			{
				cout << "\n";
			}
		}

		else if (userChoice == 'q')
		{
			cout << "You have chosen to quit the program. Thank you for using the program!\n";
		}

		else if (userChoice != 'q' || userChoice != 'p')
		{
			cout << "Invalid selection. Try again.\n\n";
		}
	}while (userChoice != 'q');

    //system("PAUSE");
	return 0;
}

int getComputerChoice()
{

	srand((unsigned int)time(NULL));
	int randomCompNum = rand() % 3 + 1;

	if (randomCompNum == 1)
	{
		cout << "The computer chose : Rock\n\n";
	}

	else if (randomCompNum == 2)
	{
		cout << "The computer chose : Paper\n\n";
	}

	else if (randomCompNum == 3)
	{
		cout << "The computer chose : Scissors\n\n";
	}

	return randomCompNum;
}

int getPlayerChoice()
{
	int myChoice;

	cout << "\n\nRock, Paper, or Scissors?\n"
		<< "1) Rock\n"
		<< "2) Paper\n"
		<< "3) Scissors\n"
		<< "Please enter your choice : \n";
	cin >> myChoice;

	if (myChoice == 1)
	{
		cout << "\nYou chose : Rock\n";
	}

	else if (myChoice == 2)
	{
		cout << "\nYou chose : Paper\n";
	}

	else if (myChoice == 3)
	{
		cout << "\nYou chose : Scissors\n";
	}

	return myChoice;

	while (myChoice < 1 || myChoice > 3)
	{
		cout << "Please pick a number between 1 & 3.\n";
		cin >> myChoice;
	}

}

bool isPlayerWinner(int myChoice, int randomCompNum)
{
	if ((myChoice == 1) && (randomCompNum == 3))
	{
		return true; 
	}

	else if ((myChoice == 3) && (randomCompNum == 2))
	{
		return true;
	}

	else if ((myChoice == 2) && (randomCompNum == 1))
	{
		return true;
	}

	else if ((randomCompNum == 3) && (myChoice == 1))
	{
		return false;
	}

	else if ((randomCompNum == 3) && (myChoice == 2))
	{
		return false;
	}

	else if ((randomCompNum == 2) && (myChoice == 1))
	{
		return false;
	}
}

bool isTie(int myChoice, int randomCompNum)
{
	if (myChoice == randomCompNum)
	{
		return true;
	}

	else if (myChoice != randomCompNum)
	{
		return false;
	}
}
Have a go at using the debugger - learn how to find problems yourself, it's immensely valuable skill :+)

There should be a GUI debugger in your IDE :+)
Compilation is always a good start, I used cpp.sh with all 3 warning levels on:

In function 'bool isPlayerWinner(int, int)':
160:1: warning: control reaches end of non-void function [-Wreturn-type]
In function 'bool isTie(int, int)':
173:1: warning: control reaches end of non-void function [-Wreturn-type]
Debugger shows no errors and I've been trying to find the problem for the two past two days. I've been working on this program for over 5 combined hours now and figured I would ask for online help as a last resort.

EDIT: I don't know what cpp.sh is honestly. I played around with the if, else if, etc and I got the code to work. Updated code below. Please let me know if you see anything wrong (We use an auto-grader which will give us a green pass if it matches the required output, but I don't want to just pass for the sake of passing.)

EDIT EDIT: Now whenever I tie, it also says I lose.

EDIT EDIT EDIT: Replaced
1
2
3
4
			if (!isPlayerWinner(playerChoice, computerChoice))
			{
				cout << "Sorry you LOSE.\n\n";
			}
with if else and everything seems good now.

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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;

int getComputerChoice(); /* FUNCTION PROTOTYPES ARE LISTED HERE */
int getPlayerChoice();
bool isTie(int, int);
bool isPlayerWinner(int, int); /* END OF FUNCTION PROTOTYPES */

int main()
{
	char userChoice; /* VARIABLES ARE DEFINED HERE*/
	int playerChoice;
	int computerChoice; /* END OF VARIABLE DEFINING */

	do
	{
		cout << "\n"
			<< "ROCK PAPER SCISSORS MENU\n"
			<< "------------------------\n"
			<< "p) Play Game\n"
			<< "q) Quit\n"
			<< "Please enter your choice : \n";
		cin >> userChoice;

		if (userChoice == 'p')
		{
			playerChoice = getPlayerChoice();
			computerChoice = getComputerChoice();

			if (isTie(playerChoice, computerChoice))
			{
				cout << "It's a TIE!\n\n";
			}

			else if (!isPlayerWinner(playerChoice, computerChoice))
			{
				cout << "Sorry you LOSE.\n\n";
			}

			else if (isPlayerWinner(playerChoice, computerChoice))
			{
				cout << "You WIN!\n\n";
			}
		}

		else if (userChoice == 'q')
		{
			cout << "You have chosen to quit the program. Thank you for using the program!\n";
		}

		else if (userChoice != 'q' || userChoice != 'p')
		{
			cout << "Invalid selection. Try again.\n\n";
		}
	}while (userChoice != 'q');

    //system("PAUSE");
	return 0;
}

int getComputerChoice()
{

	srand((unsigned int)time(NULL));
	int randomCompNum = rand() % 3 + 1;

	if (randomCompNum == 1)
	{
		cout << "The computer chose : Rock\n\n";
	}

	else if (randomCompNum == 2)
	{
		cout << "The computer chose : Paper\n\n";
	}

	else if (randomCompNum == 3)
	{
		cout << "The computer chose : Scissors\n\n";
	}

	return randomCompNum;
}

int getPlayerChoice()
{
	int myChoice;

	cout << "\n\nRock, Paper, or Scissors?\n"
		<< "1) Rock\n"
		<< "2) Paper\n"
		<< "3) Scissors\n"
		<< "Please enter your choice : \n";
	cin >> myChoice;

	if (myChoice == 1)
	{
		cout << "\nYou chose : Rock\n";
	}

	else if (myChoice == 2)
	{
		cout << "\nYou chose : Paper\n";
	}

	else if (myChoice == 3)
	{
		cout << "\nYou chose : Scissors\n";
	}

	return myChoice;

	while (myChoice < 1 || myChoice > 3)
	{
		cout << "Please pick a number between 1 & 3.\n";
		cin >> myChoice;
	}

}

bool isPlayerWinner(int myChoice, int randomCompNum)
{
	if ((myChoice == 1) && (randomCompNum == 3))
	{
		return true; 
	}

	else if ((myChoice == 3) && (randomCompNum == 2))
	{
		return true;
	}

	else if ((myChoice == 2) && (randomCompNum == 1))
	{
		return true;
	}

	else if ((randomCompNum == 3) && (myChoice == 1))
	{
		return false;
	}

	else if ((randomCompNum == 3) && (myChoice == 2))
	{
		return false;
	}

	else if ((randomCompNum == 2) && (myChoice == 1))
	{
		return false;
	}
}

bool isTie(int myChoice, int randomCompNum)
{
	if (myChoice == randomCompNum)
	{
		return true;
	}

	else if (myChoice != randomCompNum)
	{
		return false;
	}
}
Last edited on
Debugger shows no errors and I've been trying to find the problem for the two past two days.


There is a difference between a debugger and a compiler. Set up a watch list of variables, set a breakpoint, step through code 1 line at a time, deduce where it all goes wrong.

For things which are either true or false, how should an if statement look?

Compiler warnings are your friend, they tell you about things which very likely a problem.
Read the edits in the above post ^^ I didn't have any compiler errors, but I did have problems with the setup of my if, if else, etc which I corrected! Thank you very much for your help!!!! Honestly, I was getting to the point of giving up, but it feels so good to submit the assignment and have it all work perfectly.


EDIT: Ok, I did not know the cpp.sh was a C++ Shell that was part of this website. That's pretty neat! I also ran my program through there and got those same compilation messages as you did.

 In function 'bool isPlayerWinner(int, int)':
156:1: warning: control reaches end of non-void function [-Wreturn-type]
 In function 'bool isTie(int, int)':
169:1: warning: control reaches end of non-void function [-Wreturn-type]


What do I do about these?
Last edited on
I don't know what cpp.sh is honestly.


Click on the gear icon at the top right of the code - it opens a window where one can compile code. Select all 3 warnings in the option pane.

The code is still not correct. Just because it seems to work doesn't make it right.

I didn't have any compiler errors


But there are warnings, still a problem.

I am trying to get you to think about what is happening.
Yeah, I discovered what cpp.sh is now (I must say, pretty neat to have that feature)!

I did notice the warnings as well when I ran my program through the C++ shell, but I don't have the slightest clue on what to do about the warnings. I honestly don't even know what they mean. Like I'm reading them, but it's not making me think of anything.
I did some online searching and all of them mention including a return 0; in the main, but I already have a return 0; in my main??

EDIT: After looking in Microsoft Visual Studio (what we're required to use in class), it pointed out exactly where the problem was occurring (my bools at the bottom of the program). So this is correct now?

This function must return value in either case and in my prior code it returns value only if some condition is met, correct?

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
bool isPlayerWinner(int myChoice, int randomCompNum)
{
	if ((myChoice == 1) && (randomCompNum == 3))
	{
		return true; 
	}

	else if ((myChoice == 3) && (randomCompNum == 2))
	{
		return true;
	}

	else if ((myChoice == 2) && (randomCompNum == 1))
	{
		return true;
	}

	else if ((randomCompNum == 3) && (myChoice == 1))
	{
		return false;
	}

	else if ((randomCompNum == 3) && (myChoice == 2))
	{
		return false;
	}

	else if ((randomCompNum == 2) && (myChoice == 1))
	{
		return false;
	}
	return 0;
}

bool isTie(int myChoice, int randomCompNum)
{
	if (myChoice == randomCompNum)
	{
		return true;
	}

	else if (myChoice != randomCompNum)
	{
		return false;
	}
	return 0;
}
Last edited on
Pages: 12