Function

The objective of this assignment was to that each player roll a dice and the highest dice roll wins. This should allow each player to roll by hitting the "Enter" key.

My question about this code is that I am having trouble with getting response from the players such yes and no. Also, I am having problem with play one turn as I try to alternative each players but for some reason it doesn't roll and display dice. Lastly, I think I'm having problem with Display winner. Any helpful hints would help me out.

Thank you so much.

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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
 #include "header.h"

// Correct in this area! Fix other functions
int main()
{

	int rollOne;
	int rollTwo;
	char response;
	char player1[NAME_SIZE];
	char player2[NAME_SIZE];

	srand (time(NULL));

	OutputInstructions();

	GetPlayerNames(player1, player2);

	response = GetAndCheckCharResponse();

	while (response)
	{
		rollOne = PlayOneTurn(player1);
		rollTwo = PlayOneTurn(player2);
		DisplayWinner(rollOne, rollTwo, player1, player2);



		response = GetAndCheckCharResponse();
	}

	cout << "Thank you for Playing" << endl;

	return 0;
void DisplayDie(int die)
{
	const string DIE_ONE = "-----------\n"
						   "+ 		  +\n"
						   "+ 		  +\n"
						   "+ 	 0	  +\n"
						   "+ 		  +\n"
						   "+ 		  +\n"
						   "-----------\n";
	const string DIE_TWO = "-----------\n"
						   "+		  +\n"
						   "+ 	0	  +\n"
						   "+ 		  +\n"
						   "+ 		0 +\n"
						   "+ 		  +\n"
						   "-----------\n";
	const string DIE_THREE = "-----------\n"
							 "+  0 	    +\n"
							 "+		    +\n"
							 "+ 	0 	+\n"
							 "+ 		+\n"
							 "+		  0 +\n"
							 "-----------\n";
	const string DIE_FOUR = "-----------\n"
							"+ 		   +\n"
							"+ 	0 	0  +\n"
							"+ 		   +\n"
							"+ 	0 	0  +\n"
							"+ 		   +\n"
							"-----------\n";
	const string DIE_FIVE = "-----------\n"
							"+ 		   +\n"
							"+ 	0 	0  +\n"
							"+ 	  0    +\n"
							"+  0 	0  +\n"
							"+ 		   +\n"
							"-----------\n";
	const string DIE_SIX = "-----------\n"
						   "+   0 	0 +\n"
						   "+ 		  +\n"
						   "+ 	0 	0 +\n"
						   "+ 		  +\n"
						   "+ 	0 	0 +\n"
						   "-----------\n";

	if (die < 1 || die > 6)
	{
		cerr << "***** ERROR - INVALID DIE VALUE *****\n";
	}
	else
	{
		cout << "EDGE << DIE[die-1] << EDGE";
	}
}
int DisplayWinner(int rollOne, int rollTwo, char* player1, char* player2)
{
	char winner;

	if (rollOne > rollTwo)
	{
		cout << " " << player1 << " is the WINNER!!!" << endl;
		winner = player1;
	}
	else if (rollTwo > rollOne)
	{
		cout << " " << player2 << " is the WINNER!!!" << endl;
		winner = player2;
	}
	else
	{
		cout << " " << player1 << "and" << " " << player2
			 << "have tied!" << endl;
		winner = player1, player2;
	}


	return winner;
}
bool GetAndCheckCharResponse()
{

	bool valid;
	char checkResponse;


	do
	{
		cout << "Would you like to play High Roller (y/n)? ";
		cin.get(checkResponse);
		cin.ignore(1000, '\n');
		valid = checkResponse == 'Y' || checkResponse == 'y'
				|| checkResponse == 'N'  || checkResponse == 'n';

		if (!valid)
		{
			cout << "**Invalid Input - Please enter 'Y' or 'N' **";

		}
	}
	while (!valid);

	return checkResponse;
}
void GetPlayerNames(char player1[NAME_SIZE],
					 char player2[NAME_SIZE])
{
	cout << "First player, please enter your name: ";
	cin.getline(player1, NAME_SIZE);
	cin.ignore(1000, '\n');

	cout << "Second player, please enter your name: ";
	cin.getline(player2, NAME_SIZE);
	cin.ignore(1000, '\n');

}
int PlayOneTurn(char player[])
{
	int die;
	int roll;
	char player1;
	char player2;


	cout << " " << player1 << "\'s turn. ";

	PressEnterToRoll();

	DisplayDie(die);

	die = RollDie(roll);


	cout << " " << player2 << " \'s turn. ";

	PressEnterToRoll();

	die = RollDie(roll);

	DisplayDie(die);

	return die;
}
void PressEnterToRoll()
{
	string rollDice;
	cout << "Please <enter> to roll...";
	cin >> rollDice;

}

int RollDie(int roll)
{
	roll = rand() % 6 + 1;

	return roll;
}




Last edited on
Can soneone help me out here? I think there something wrong in this area.
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
int PlayOneTurn(char player[])
{
	int die;
	int roll;
	char player1;
	char player2;


	cout << " " << player1 << "\'s turn. ";

	PressEnterToRoll();

	DisplayDie(die);

	die = RollDie(roll);


	cout << " " << player2 << " \'s turn. ";

	PressEnterToRoll();

	die = RollDie(roll);

	DisplayDie(die);

	return die;
}
1
2
DisplayDie(die);
die = RollDie(roll);
You are trying to display die before it was thrown.
Okay, my next problem is that whenever I try to press enter, it would not roll.

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
int main()
{

	int rollOne;
	int rollTwo;
	char response;
	char player1[NAME_SIZE];
	char player2[NAME_SIZE];

	srand (time(NULL));

	OutputInstructions();

	GetPlayerNames(player1, player2);

	response = GetAndCheckCharResponse();

	while (response)
	{
		rollOne = PlayOneTurn(player1);
		rollTwo = PlayOneTurn(player2);
		DisplayWinner(rollOne, rollTwo, player1, player2);

		response = GetAndCheckCharResponse();
	}

	cout << "Thank you for Playing" << endl;

	return 0;

void PressEnterToRoll()
{
	char rollDice;
	char player[NAME_SIZE];


	cout << "Please <enter> to roll...";
	cin >> rollDice;

	rollDice = PlayOneTurn(player);

}
int PlayOneTurn(char player[])
{
	int die;
	int roll;


	cout << " " << player << "\'s turn. ";

	PressEnterToRoll();
	die = RollDie(roll);

	DisplayDie(die);


	return die;
}


Any hint and ideas please?
Well, cin requires an input to continue, so hitting enter in that case will not work. Try using getline.
Topic archived. No new replies allowed.