C++ Rock, Paper, Scissors Function Woes

Pages: 12
I've about reached the limit of help that I personally would provide to someone who seems to be putting minimal effort into debugging their own code. We've helped you with several bugs, and each time, you turn back to the forum immediately after running the new program, without demonstrating approaches you've taken to try and resolve whatever bug shows up next.

I'll provide you some help here, but it may not be the help you want. I'm doing you a favor here by saying that TheIdeasMan actually pointed you in the right direction with the warning levels. Look at what the error messages mean, and what parts of your program they highlight.

Additionally, consider ways to see the inner workings of what your program is doing, that will allow you to test and debug your own code. Think to yourself, what can I do to my program to see what the value of a variable is at a certain point in the program? How might I show what value a function is returning to make sure it's doing what I expect it to? This is an immensely useful concept in debugging.
Last edited on
What happens in the isPlayerWinner function when both arguments are the same?

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


Think about what happens, trace the values on paper, otherwise:

USE THE DEBUGGER !!

I do apologize, mastakhan. I honestly can't put into words how thankful and appreciative I am of the help you guys are giving me. I'm at a point where I'm so fed up with trying to figure out the issues with this program on my own that I almost gave up hence why it appeared that I was giving it minimal effort.

TheIdeasMan really helped me and thanks to the debugger I sorted out the warnings. One thing I would like to mention is that the powerpoint lectures which our professor provided us with did not mention that we need to return a value in any case.

#include <iostream>
using namespace std;
//function prototype
bool isEven(int);
int main()
{
int value;
cout << "Please enter an integer number: ";
cin >> value;
if(isEven(value)) //function call is used as a boolean expr
{
cout << value << " is an even number." << endl;
}
else
{
cout << value << " is an odd number." << endl;
}
system("PAUSE");
return 0;
}
/*The isEven function returns true if the parameter is even and false otherwise*/
bool isEven(int number)
{
if((number % 2) != 0)
return false; //the number is odd if there’s a remainder
else
return true; //otherwise its even
}



This is the slide from our lecture I was using to help me which should explain to you guys why I was so confused with the whole warning thing because we never actually learned that we need to return a value in any case and not just specific case.


CODE WITH ALL ERRORS/BUGS/WARNINGS FIXED:

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
#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;
	}
	return 0;
}

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

	else if (myChoice != randomCompNum)
	{
		return false;
	}
	return 0;
}
Last edited on
l would prefer this:

1
2
3
4
5
6
7
else if ((randomCompNum == 2) && (myChoice == 1))
	{
		return false;
	}
	return 0;
       else {return false;}
}


Why did I have an else statement?
"Why did I have an else statement?"

The function should always return SOME value and the else at the end would make sure of that, 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
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
180
#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;
	}

	else
	{
		return false;
	}
}

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

	else if (myChoice != randomCompNum)
	{
		return false;
	}

	else
	{
		return false;
	}
}
Topic archived. No new replies allowed.
Pages: 12