Void function for Rock Paper Scissors

Hello. I'm pretty stuck on my program and I need help. I'm having trouble mainly with the void function I'm required to put in it. I'll place the code here. Also, it is not done yet. I just need it to compile before I can go to the next step.

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

#include <cctype>
#include <cstdlib>
#include <ctime>

//Subprograms.

char getUser()
{
char user2 = 'X'; //Default value.
  while(true)
  {
    cout << "Rock, paper, scissors? [R,P,S,Q to exit]" << endl;
	cin >> user2;
	cin.ignore(1000,10);
	
	user2 = toupper(user2);
	
	if (user2 == 'Q') 
	break;
	
	
	else if (user2 == 'R' || user2 == 'P' || user2 == 'S')
	return user2;
	
	else
	  cout << "Invalid entry." << endl;
  }
}

char getUnit()
{
char unit2 = 'X'; //Default value.
  while(true)
  {
    srand(time(0));
    char unitneeded  = rand() % 3;
  
    if(unitneeded == 1)
      unit2 = 'R';
	
    else if(unitneeded == 2)
      unit2 = 'P';
	
    else if (unitneeded == 3)
	  unit2 = 'S';
  }
	  
  return unit2;
  }
  
void winner()
  {
    if (user == 'R' && unit == 'R')
      cout << "Tie." << endl;
	  
    else if (user == 'R' && unit == 'S')
      cout << "User wins." << endl;
	
	else if (user == 'R' && unit == 'P')
	  cout << "Computer wins." << endl;
	  
	else if (user == 'P' && unit == 'P')
	  cout << "Tie." << endl;
	  
	else if (user == 'P' && unit == 'S')
	  cout << "Computer wins." << endl;
	  
	else if (user == 'P' && unit == 'R')
	  cout << "User wins." << endl;
	  
	else if (user == 'S' && unit == 'S')
	  cout << "Tie." << endl;
	  
	else if (user == 'S' && unit == 'P')
	  cout << "User wins." << endl;
	  
	else if (user == 'S' && unit == 'R')
	  cout << "Computer wins." << endl;
	  
  }
  
int main()
{  
  	
  srand(time(0)); rand();

  // declare variables
  char user = getUser();
  char unit = getUnit();
  
	winner ();
 
  

}


When I try to compile it says user and unit are undeclared identifiers in the void function.
Your winner() function isn't aware of the existence of the user or unit variables.

They only exist within the scope of the function in which they're declared, which is main().

In order for your code to work, you'd need to pass them into the function as parameters.
Thanks I'll try that. :D I figured out I accidentally skipped some steps in the program so I took some out and went back, but now I'm stuck again. Keep in mind it's still quite unfinished.

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

#include <cctype>
#include <cstdlib>
#include <ctime>

//Subprograms.

char getUser()
{
char user2 = 'X'; //Default value.
  while(true)
  {
    cout << "Rock, paper, scissors? [R,P,S,Q to exit]" << endl;
	cin >> user2;
	cin.ignore(1000,10);
	
	user2 = toupper(user2);
	
	if (user2 == 'Q') 
	break;
	
	else if(user2 == 'R')
	  cout << "You chose rock." << endl;
	  
	  
	else if(user2 == 'S')
	  cout << "You chose scissors." << endl;
	  
	  
	else if(user2 == 'P')
	  cout << "You chose paper." << endl;
	  
	
	else
	  cout << "Invalid entry." << endl;
	  
	return user2;
  }
}

char getUnit()
{
  char unit2;
  unit2  == 'R';
  
  if(unit2 == 'R')
    cout << "The computer chose rock." << endl;
	
  else if(unit2 == 'S')
	  cout << "The computer chose scissors." << endl;
	  
  else if(unit2 == 'P')
	  cout << "The computer chose paper." << endl;
	
  return unit2;
}
  
void winner()
  {
    
	  
  }
  
int main()
{  
  // initialize the computer's random number generator	
  srand(time(0)); rand();

  // declare variables
  char user;
  char unit;
  // start loop
  while(true)
  {
    // determine computer's choice
	char unit = getUnit();
    // prompt for, and read, the human's choice
	char user = getUser();
    // if human wants to quit, break out of loop
	if (user == 'Q') break;
    // print results
	winner ();
  }
  // end loop
  // end program
  

}


My new problem is I'm stuck in an infinite loop on the user's input. It outputs the user's input and then asks again and skips the rest of the program including the computer's output.
What does this function return if the user enters 'Q'? Point to the line containing that return.

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
char getUser()
{
char user2 = 'X'; //Default value.
  while(true)
  {
    cout << "Rock, paper, scissors? [R,P,S,Q to exit]" << endl;
	cin >> user2;
	cin.ignore(1000,10);
	
	user2 = toupper(user2);
	
	if (user2 == 'Q') 
	break;
	
	else if(user2 == 'R')
	  cout << "You chose rock." << endl;
	  
	  
	else if(user2 == 'S')
	  cout << "You chose scissors." << endl;
	  
	  
	else if(user2 == 'P')
	  cout << "You chose paper." << endl;
	  
	
	else
	  cout << "Invalid entry." << endl;
	  
	return user2;
  }
}

It returns Q and in the int main it quits the program. I tried adding more breaks, but the compiler gives me an error when I do saying illegal else without matching if, so I changed the else if to if statements, but then the else says the same error.

Update; Got it to work! I switched the loop that was giving me trouble into a switch statement.
Last edited on
Why are you useing so many breaks? You can just use a return value.

Try this:

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
char getUser()
{
    char user2 = 'X'; //Default value.
    cout << "Rock, paper, scissors? [R,P,S,Q to exit]" << endl;
    cin >> user2;
    cin.ignore(1000,10);
    user2 = toupper(user2);
    if (user2 == 'Q')
    {
        return user2;
    }
    else if(user2 == 'R')
    {
        cout << "You chose rock." << endl;
        return user2;
    }
    else if(user2 == 'S')
    {
        cout << "You chose scissors." << endl;
        return user2;
    }
    else if(user2 == 'P')
    {
        cout << "You chose paper." << endl;
        return user2;
    }
    else
    {
        cout << "Invalid entry." << endl;
        return user2;
    }  
    return user2;
}


Also, you are forgetting all of your brackets in the if-statements.
Last edited on
It returns Q

No, it does not. Let's look at the code again.

1
2
if (user2 == 'Q') 
	break;


If user2 is Q, then the while loop is broken out of and THERE IS NO return. The only return is INSIDE the while loop.
I've finished the coding but I'm still having two problems.

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

#include <cctype>
#include <cstdlib>
#include <ctime>

//Subprograms.

char getUser()
{
char user2 = 'X'; //Default value.
  while(true)
  {
    cout << "Rock, paper, scissors? [R,P,S,Q to exit]" << endl;
	cin >> user2;
	cin.ignore(1000,10);
	
	if (user2 == 'Q' || user2 == 'q')
	break;
	
	return user2;
  }

}

char getUnit()
{
  char unit2 = 'X'; //Default value.
  while(true)
  {
    int unitneeded  = rand() % 3;
  
    if(unitneeded == 0)
      unit2 = 'R';
	
    else if(unitneeded == 1)
      unit2 = 'P';
	
    else if (unitneeded == 2)
	  unit2 = 'S';
  
	}  
  
	
  return unit2;
}
  
void winner()
  {
    if (user == 'R' && unit == 'R')
      cout << "Tie." << endl;
	  
    else if (user == 'R' && unit == 'S')
      cout << "User wins." << endl;
	
	else if (user == 'R' && unit == 'P')
	  cout << "Computer wins." << endl;
	  
	else if (user == 'P' && unit == 'P')
	  cout << "Tie." << endl;
	  
	else if (user == 'P' && unit == 'S')
	  cout << "Computer wins." << endl;
	  
	else if (user == 'P' && unit == 'R')
	  cout << "User wins." << endl;
	  
	else if (user == 'S' && unit == 'S')
	  cout << "Tie." << endl;
	  
	else if (user == 'S' && unit == 'P')
	  cout << "User wins." << endl;
	  
	else if (user == 'S' && unit == 'R')
	  cout << "Computer wins." << endl;
	  
  }
  
int main()
{  
  // initialize the computer's random number generator	
  srand(time(0)); rand();

  // declare variables
  char user;
  char unit;
  // start loop
  while(true)
  {
    // determine computer's choice
	char unit = getUnit();
    // prompt for, and read, the human's choice
	char user = getUser();
    // if human wants to quit, break out of loop
	if (user == 'Q') break;
    // print results
	
	switch(user)
	{
	
	case 'R':
	case 'r':
	  cout << "You chose rock." << endl;
	  break;
	  
	case 'S':
	case 's':
	  cout << "You chose scissors." << endl;
	  break;
	  
	  
	case 'P':
	case 'p':
	  cout << "You chose paper." << endl;
	  break;
	  
	
	default:
	  cout << "Invalid entry." << endl;
	  
	}
	switch(unit)
	{
	
	case 'R':
	  cout << "The computer chose rock." << endl;
	  break;
	  
	case 'S':
	  cout << "The computer chose scissors." << endl;
	  break;
	  
	  
	case 'P':
	  cout << "The computer chose paper." << endl;
	  break;
	  
	 }
	  
	winner ();
  }
  // end loop
  // end program
  

}


First program is it won't compile cause I don't have user or unit in the void and I don't know how to write the parameters to be in void. Sorry. Also, when it was compiling before it wouldn't run and would exit the program right away or there'd just be a blank spot that I couldn't press any key on.

Edit; It compiles but won't run.. Thank you very much for your help.
Last edited on
I don't have user or unit in the void and I don't know how to write the parameters to be in void


You need to go back to your source for learning and take another look at functions. You can pass variables into the function.
Aren't you forgetting a lot of brackets?
In addition to what Moschops and IWishIKnew mentioned, you have a few other problems:
L20: The break is going to exit out of the while loop, but there is no return statement after the while loop. Most compilers will generate a return 0. Not what you want.
L22: What if the user didn't enter R,P or S? You're going to return that.
L30: What's the point of the while loop here? You're never going to exit this loop.
L99: You allow the user input to be upper or lower case in your switch statement, but in winner(), you're only checking for upper case.
Line 86,87: Unneeded. You've declared user and unit at 92 and 94.
Line 49: You need to pass unit and used into your winner function.
void winner (char unit, char user)
Line 51,60,69: There's an easier way to check for a tie.
if (user == unit)
Linr 19 & 96: You allow the user to enter 'Q' or 'q', but only check 'Q' to exit.
Thank you for the help guys. I've finally got it working. Thank you so much.
Topic archived. No new replies allowed.