Help with functions & rand() please?

Can someone help me with this problem? It's a rock, paper, scissors game with the computer and the user. I'm having issues with the random number generator and the functions (I think primarily the void displayChoice function)
Thanks for any tips or guidance!


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
#include <iostream>
#include <cstdlib>
#include <time.h>
using namespace std;

char getUserChoice()
{
	char userChoice;
	cout << "Choose R (rock), P (paper), or S (scissors): ";
	cin >> userChoice;

	return userChoice;
}

void displayChoice(char uChoice)
{
	if (uChoice == 'R' || uChoice == 'r')
	{
		cout << "Rock";
	}
	else if (uChoice == 'P' || uChoice == 'p')
	{
		cout << "Paper";
	}
	else if (uChoice == 'S' || uChoice == 's')
	{
		cout << "Scissors";
	}
}

char getComputerChoice()
{
	srand(time(0));
	
	int cChoice = (rand() % 3) + 1;
	switch (cChoice)
	{
	case 1: return 'R';
			break;
	case 2: return 'P';
			break;
	case 3: return 'S';
			break;
	}
}


char getBiasedComputerChoice()
{
	int num = 1 + rand() % (3 - 1 + 1);
	switch (num)
	{
	case 1: return 'R';
	case 2: return 'P';
	case 3:
		int n = 1 + rand() % (2 - 1 + 1);
		if (n == 1)
		{
			return 'R';
		}
		else if (n == 2)
		{
			return 'P';
		}
	}
}

char determineWinner()
{
	char cChoice, uChoice;
	if (uChoice == 'R')
	{
		if (cChoice == 'P')
		{
			cout << "Paper covers Rock. Computer wins.";
		}
		else if (cChoice == 'S')
		{
			cout << "Rock smashes Scissors. User wins.";
		}
		else if (cChoice == 'R')
		{
			cout << "Both choose Rock. Tie.";
		}
	}
	else if (uChoice == 'S')
	{
		if (cChoice == 'P')
		{
			cout << "Scissors cuts Paper. User wins.";
		}
		else if (cChoice == 'S')
		{
			cout << "Both choose Scissors. Tie.";
		}
		else if (cChoice == 'R')
		{
			cout << "Rock smashes Scissors. Computer wins.";
		}
	}
	else if (uChoice == 'P')
	{
		if (cChoice == 'P')
		{
			cout << "Both choose Paper. Tie.";
		}
		else if (cChoice == 'S')
		{
			cout << "Scissors cuts Paper. Computer wins.";
		}
		else if (cChoice == 'R')
		{
			cout << "Paper covers Rock. User wins.";
		}
	}
}


	int main()
{
	// play 10 Games between user and computer 
	for (int i = 0; i < 10; i++)
	{
		getUserChoice();
		displayChoice();
		getComputerChoice();
		determineWinner();
	}

	
	// play 10 Games between user and the biased computer 
	for (int i = 0; i < 10; i++)
	{
		getUserChoice();
		displayChoice();
		getComputerChoice();
		determineWinner();
	}
	

	system("pause");
	return 0;
}
At line 125 & 135 your not passing any value to the function. I'm surprised if it compiles.
Last edited on
It doesn't. But I left it empty for the sake of this post, because when I change it to displayChoice (char uChoice) I get even more errors, such as "expected a ')'". Would you recommend I put the displayChoice(); inside the getUserChoice() function, instead of the main function? I'm still trying to get the hang of functions clearly..
Topic archived. No new replies allowed.