Ye ole Rock-Paper-Scissors game, Creating/Calling functions

Hello, I am new to programming and need help with an oldie. I have read many other posts about the Rock-Paper-Scissors program, but cannot locate an answer for my difficulty...

I am using Visual Studio 2017. My code compiles and runs, but does not run properly passed my Input Validation. The goal is to create and call functions for the different steps of the program.

Lines 64-85 were given, except I renamed the variable on lines 84 and 87 to compChoice.

Any insight or suggestions would be appreciated. Thank you in advance.



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

// Function Prototypes.
int getComputerChoice();
void compChooseRock();
void compChoosePaper();
void compChooseScissors();
void noWinner();
void validateEntry();

// Declare constants.
const int ROCK = 1;
const int PAPER = 2;
const int SCISSORS = 3;

// Declare variables. 
int userChoice, compChoice;

int main()
{
	// Display instructions to user. 
	cout << " I challenge you to Rock-Paper-Scissors! \n\n";
	cout << " Select Rock, Paper, or Scissors. \n\n";
	cout << " Press   1 for Rock\n\t 2 for Paper\n\t 3 for Scissors\n\n";
	cout << " Choose wisely...\n\n";
	cout << " Enter your selection: ";
	cin >> userChoice;

	// Call computer's choice.
	getComputerChoice();

	// User input validation.
	if (userChoice != 1 && userChoice != 2 && userChoice != 3)
	{
		validateEntry();
	}
	// Computer generates Rock.
	else if (compChoice == ROCK)
	{
		compChooseRock();
	}
	// Computer generates Paper.
	else if (compChoice == PAPER)
	{
		compChoosePaper();
	}
	// Computer generates Scissors.
	else if (compChoice == SCISSORS)
	{
		compChooseScissors();
	}
	// User selection matches generated selection. 
	else if (compChoice == userChoice)
	{
		noWinner();
	}

	system("pause");
	return 0;
}
// ********************************************************
// The getComputerChoice function returns the computer's  *
// game choice. It returns 1 for rock (via the ROCK       *
// constant), or 2 for paper (via the PAPER constant),    *
// or 3 for scissors (via the SCISSORS constant).         *
// ********************************************************
int getComputerChoice()
{
	// Get the system time so we can use it
	// to seed the random number generator.
	unsigned seed = time(0);

	// Use the seed value to seed the random
	// number generator.
	srand(seed);

	// Generate a random number in the range of 1-3.
	int compChoice = 1 + rand() % 3;

	// Return the random value.
	return compChoice;
}
// Function for compChioce = 1.
void compChooseRock()
{
	if (userChoice == SCISSORS)
	{
		cout << " I chose Rock" << endl;
		cout << " You chose Scissors" << endl;
		cout << " Rock breaks Scissors\n";
		cout << " You loose... Better luck next time.\n" << endl;
	}
	else if (userChoice == PAPER)
	{
		cout << " I chose Rock" << endl;
		cout << " You chose Paper" << endl;
		cout << " Paper covers Rock\n";
		cout << " Congratulations! You WIN!\n" << endl;
	}
}
// Function for compChoice = 2.
void compChoosePaper()
{
	if (userChoice == ROCK)
	{
		cout << " I chose Paper" << endl;
		cout << " You chose Rock" << endl;
		cout << " Paper covers Rock\n";
		cout << " You loose... Better luck next time.\n" << endl;
	}
	else if (userChoice == SCISSORS)
	{
		cout << " I chose Paper" << endl;
		cout << " You chose Scissors" << endl;
		cout << " Scissors cut Paper\n";
		cout << " Congratulations! You WIN!\n" << endl;
	}
}
// Function for compChoice = 3.
void compChooseScissors()
{
	if (userChoice == PAPER)
	{
		cout << " I chose Scissors" << endl;
		cout << " You chose Paper" << endl;
		cout << " Scissors cut Paper\n";
		cout << " You loose... Better luck next time.\n" << endl;
	}
	else if (userChoice == ROCK)
	{
		cout << " I chose Scissors" << endl;
		cout << " You chose Rock" << endl;
		cout << " Rock breaks Scissors\n";
		cout << " Congratulations! You WIN!\n" << endl;
	}
}
// Function for compChoice = userChoice.
void noWinner()
{
	{
		cout << " We chose the same.\n" << endl;
		cout << " It is a TIE!" << endl;
	}
}
// Function for improper input. 
void validateEntry()
{
	cout << " Invalid entry. Please try again.\n" << endl;
}
Last edited on
The problem is on line 33. You never set the compChoice variable in the main() function to the value returned by getComputerChoice(). It should be this:
 
compChoice = getComputerChoice();


Also, I made your code a bit 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
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
#include <iostream>
#include <ctime>
#include <cmath>
using namespace std;

// Function Prototypes.
int getComputerChoice();
void compChooseRock();
void compChoosePaper();
void compChooseScissors();
void noWinner();
void validateEntry();


// Declare constants (constexpr is supported for compile time constants since C++11)
constexpr int ROCK = 1;
constexpr int PAPER = 2;
constexpr int SCISSORS = 3;

// Declare variables. 
int userChoice, compChoice;

int main()
{
	// Display instructions to user. 
	cout << " I challenge you to Rock-Paper-Scissors! \n\n";
	cout << " Select Rock, Paper, or Scissors. \n\n";
	cout << " Press   1 for Rock\n\t 2 for Paper\n\t 3 for Scissors\n\n";
	cout << " Choose wisely...\n\n";
	cout << " Enter your selection: ";
	cin >> userChoice;

	// Call computer's choice.
	compChoice = getComputerChoice();
	// User input validation.
	if (userChoice != 1 && userChoice != 2 && userChoice != 3)
		validateEntry();
	
	switch (compChoice)
	{
	    case ROCK: compChooseRock(); break;
	    case PAPER: compChoosePaper(); break;
	    case SCISSORS: compChooseScissors(); break;
	    default: noWinner();
	}

	system("pause");
	return 0;
}
// ********************************************************
// The getComputerChoice function returns the computer's  *
// game choice. It returns 1 for rock (via the ROCK       *
// constant), or 2 for paper (via the PAPER constant),    *
// or 3 for scissors (via the SCISSORS constant).         *
// ********************************************************
int getComputerChoice()
{
	// Get the system time so we can use it
	// to seed the random number generator.
	unsigned seed = time(0);

	// Use the seed value to seed the random
	// number generator.
	srand(seed);

	// Generate a random number in the range of 1-3.
	int compChoice = 1 + rand() % 3;

	// Return the random value.
	return compChoice;
}
// Function for compChioce = 1.
void compChooseRock()
{
	if (userChoice == SCISSORS)
	{
		cout << " I chose Rock" << endl;
		cout << " You chose Scissors" << endl;
		cout << " Rock breaks Scissors\n";
		cout << " You loose... Better luck next time.\n" << endl;
	}
	else if (userChoice == PAPER)
	{
		cout << " I chose Rock" << endl;
		cout << " You chose Paper" << endl;
		cout << " Paper covers Rock\n";
		cout << " Congratulations! You WIN!\n" << endl;
	}
}
// Function for compChoice = 2.
void compChoosePaper()
{
	if (userChoice == ROCK)
	{
		cout << " I chose Paper" << endl;
		cout << " You chose Rock" << endl;
		cout << " Paper covers Rock\n";
		cout << " You loose... Better luck next time.\n" << endl;
	}
	else if (userChoice == SCISSORS)
	{
		cout << " I chose Paper" << endl;
		cout << " You chose Scissors" << endl;
		cout << " Scissors cut Paper\n";
		cout << " Congratulations! You WIN!\n" << endl;
	}
}
// Function for compChoice = 3.
void compChooseScissors()
{
	if (userChoice == PAPER)
	{
		cout << " I chose Scissors" << endl;
		cout << " You chose Paper" << endl;
		cout << " Scissors cut Paper\n";
		cout << " You loose... Better luck next time.\n" << endl;
	}
	else if (userChoice == ROCK)
	{
		cout << " I chose Scissors" << endl;
		cout << " You chose Rock" << endl;
		cout << " Rock breaks Scissors\n";
		cout << " Congratulations! You WIN!\n" << endl;
	}
}
// Function for compChoice = userChoice.
void noWinner()
{
	{
		cout << " We chose the same.\n" << endl;
		cout << " It is a TIE!" << endl;
	}
}
// Function for improper input. 
void validateEntry()
{
	cout << " Invalid entry. Please try again.\n" << endl;
}
Last edited on
Thank you IceStorm. I need to learn how switch works. That shortened the code considerably. I will study your example. In my code, I changed line 33, made line 41 an if, lines 80 and 83 changed the variable name back to the original designation number, and it ran.

However, (referencing my code) when I run it multiple times, occasionally it hangs up. If my input was 1, 2, 3, 5, e, 1, 2, 3, 8, t, 1, 2, 3, g, it seems to not run the remaining code on one of the valid inputs like the second time I enter 2. Yet works properly on the first and third 2. Any thoughts on why it seems to fail randomly?
Never mind. I found it! After entering 1, 1, 1, .... enough times, I had never reached the noWinner. I placed it as my second if on line 36. My code was never reaching line 56 to check if it was a tie.
Topic archived. No new replies allowed.