dice 21

This program is about dice 21. The problem i have is when i go to the second round it adds up the total turn value i got from the first round, and I dont want that to happen I want it to start again at 0. I still need to more to the program but I will do that myself I just need for some help to fic the problem.

If you pass 21 you lose and if your total score is 21 you win







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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#include<iostream>
#include<cstdlib>
#include<time.h>

using namespace std;

int getRandomNum( int lowRange, int highrange);

int main()
{

char choice=' ';
char keep=' ';


srand( static_cast<int>( time(NULL) ) );

///////////////////////////// Start of the code

cout<<"**************** Welcome to the Dice 21 game! ****************"<<endl;
cout<<endl;


cout<<"You must win the majority of rounds to win the game. In each";
cout<<"round you will roll one 10-sided die as many times as you like.";
cout<<"Each time you roll, the number on the die will be added to your";
cout<<"total score. Your goal is to get a score as close as possible to";
cout<<"21 without going over 21.";
cout<<endl;
cout<<endl;

cout<<"At the end of a round, you will be able to look ahead to see";
cout<<" what the next roll would have been. You win the round if the";
cout<<" next roll would have caused you to go bust. Otherwise you lose";
cout<<" the round"<<endl;
cout<<endl;

cout<<"********** Round 1 *********"<<endl;


int rollValue= 0;
int total_turn= 0;
int value1= 0;
int value2= 0;




do
{
	if(rollValue==1)
	{
		cout<<"You have rolled a 1. It's value could be 1 or 11"<<endl;
		cout<<"Enter 'y' if you would like the value to be 11"<<endl;
		cout<<"Enter any other character if you would like to keep the value 1"<<endl;
		cin>> keep;
		if(keep=='y')
		{
			value1= 11;
			cout<<"You rolled:"<<value1<<endl;
			total_turn= total_turn + value1;
			cout<<"Your roll total is:"<<total_turn<<endl;
			cout<<endl;
		}
		else
		{
		value2= 1;
		cout<<"You rolled:"<<value2<<endl;

		total_turn= total_turn+ value2;
		cout<<"Your roll total is :"<< total_turn<<endl;
		cout<<endl;
		}
	}

	cout<<"Rolling..."<<endl;
	rollValue = rand() % 10 + 1;
	cout<<"You rolled a :"<<rollValue<<endl;
	total_turn= total_turn + rollValue;
	cout<<"Your roll tatal is :"<<total_turn<<endl;
	cout<<endl;


	cout<<"Do you want to roll again: (y/n)";
	cin>> choice;

	if(total_turn==21)
	{
		cout<<"WINNERR YOU ROLLED 21!!!"<<endl;
		break;
		
	}
	else if(total_turn>21)
	{
		cout<<"Sorry you lose you rolled over 21"<<endl;
		break;

		
	

	}

	if(choice=='y')
	{

if(rollValue==1)
	{
		cout<<"You have rolled a 1. It's value could be 1 or 11"<<endl;
		cout<<"Enter 'y' if you would like the value to be 11"<<endl;
		cout<<"Enter any other character if you would like to keep the value 1"<<endl;
		cin>> keep;
		if(keep=='y')
		{
			value1= 11;
			cout<<"You rolled:"<<value1<<endl;
			total_turn= total_turn + value1;
			cout<<"Your roll total is:"<<total_turn<<endl;
			cout<<endl;
		}
		else
		{
		value2= 1;
		cout<<"You rolled:"<<value2<<endl;
		
		total_turn= total_turn+ value2;
		cout<<"Your roll total is"<< total_turn<<endl;
		cout<<endl;
		}
}

	cout<<"Rolling..."<<endl;
	rollValue = rand() % 10 + 1;
	cout<<"You rolled a :"<<rollValue<<endl;
	total_turn= total_turn + rollValue;
	cout<<"Your roll tatal is :"<<total_turn<<endl;
	if(total_turn==21)
	{
		cout<<"WINNERR YOU ROLLED 21!!!"<<endl;
		break;
		
	}
	else if(total_turn>21)
	{
		cout<<"Sorry you lose you rolled over 21"<<endl;
		break;
		
	}

	cout<<endl;
	cout<<"Do you want to roll again: (y/n)";
	cin>> choice;



	}
	else if(choice=='n')
	{
		cout<<"Your roll total is:"<<total_turn<<endl;
		rollValue = rand() % 10 + 1;
			cout<<"Look ahead to the next roll..."<<endl;
			cout<<"Your next roll would have been:"<<rollValue<<endl;
			total_turn=total_turn+rollValue;
		if(total_turn>21)
		{
			cout<<"Good choice you would have one BUST, if you have rolled again"<<endl;
			break;
		}
		else if(total_turn<21)
		{
			cout<<"BUMMER- you could have rolled again"<<endl;
			break;
		}

	

	}
	else
	{
		cout<<"invalid input"<<endl;

		break;

	}
}while(total_turn<= 21);


cout<<"********** Round 2 *********"<<endl;


do
{
	if(rollValue==1)
	{
		cout<<"You have rolled a 1. It's value could be 1 or 11"<<endl;
		cout<<"Enter 'y' if you would like the value to be 11"<<endl;
		cout<<"Enter any other character if you would like to keep the value 1"<<endl;
		cin>> keep;
		if(keep=='y')
		{
			value1= 11;
			cout<<"You rolled:"<<value1<<endl;
			total_turn= total_turn + value1;
			cout<<"Your roll total is:"<<total_turn<<endl;
			cout<<endl;
		}
		else
		{
		value2= 1;
		cout<<"You rolled:"<<value2<<endl;

		total_turn= total_turn+ value2;
		cout<<"Your roll total is :"<< total_turn<<endl;
		cout<<endl;
		}
	}

	cout<<"Rolling..."<<endl;
	rollValue = rand() % 10 + 1;
	cout<<"You rolled a :"<<rollValue<<endl;
	total_turn= total_turn + rollValue;
	cout<<"Your roll tatal is :"<<total_turn<<endl;
	cout<<endl;


	cout<<"Do you want to roll again: (y/n)";
	cin>> choice;

	if(total_turn==21)
	{
		cout<<"WINNERR YOU ROLLED 21!!!"<<endl;
		
	}
	else if(total_turn>21)
	{
		cout<<"Sorry you lose you rolled over 21"<<endl;
		
	

	}

	if(choice=='y')
	{

if(rollValue==1)
	{
		cout<<"You have rolled a 1. It's value could be 1 or 11"<<endl;
		cout<<"Enter 'y' if you would like the value to be 11"<<endl;
		cout<<"Enter any other character if you would like to keep the value 1"<<endl;
		cin>> keep;
		if(keep=='y')
		{
			value1= 11;
			cout<<"You rolled:"<<value1<<endl;
			total_turn= total_turn + value1;
			cout<<"Your roll total is:"<<total_turn<<endl;
			cout<<endl;
		}
		else
		{
		value2= 1;
		cout<<"You rolled:"<<value2<<endl;
		
		total_turn= total_turn+ value2;
		cout<<"Your roll total is"<< total_turn<<endl;
		cout<<endl;
		}
}

	cout<<"Rolling..."<<endl;
	rollValue = rand() % 10 + 1;
	cout<<"You rolled a :"<<rollValue<<endl;
	total_turn= total_turn + rollValue;
	cout<<"Your roll tatal is :"<<total_turn<<endl;
	if(total_turn==21)
	{
		cout<<"WINNERR YOU ROLLED 21!!!"<<endl;
		
	}
	else if(total_turn>21)
	{
		cout<<"Sorry you lose you rolled over 21"<<endl;
		
	}

	cout<<endl;
	cout<<"Do you want to roll again: (y/n)";
	cin>> choice;



	}
	else if(choice=='n')
	{
		cout<<"Your roll total is:"<<total_turn<<endl;
		rollValue = rand() % 10 + 1;
			cout<<"Look ahead to the next roll..."<<endl;
			cout<<"Your next roll would have been:"<<rollValue<<endl;
			total_turn=total_turn+rollValue;
		if(total_turn>21)
		{
			cout<<"Good choice you would have one BUST, if you have rolled again"<<endl;
		}
		else if(total_turn<21)
		{
			cout<<"BUMMER- you could have rolled again"<<endl;
		}

	

	}
	else
	{
		cout<<"invalid input"<<endl;
		break;

	}
}while(total_turn<= 21);



system("pause");
return 0;
}




Last edited on
1
2
3
4
5
6
7
8
9
// ...
cout<<"********** Round 2 *********"<<endl;

total_turn = 0; /// make total_turn 0 when you start round 2

do
{
	if(rollValue==1)
// ... 
thanks Vin!!!! By the way is there any other way I could make my loops shorter I feel like they are too long.
I don't know about making each loop shorter but since round 1 code is virtually the same as round 2 code you could probably make a loop to repeat round 1 code instead.

Also in your original code you should make rollValue = 0 for the start of round 2 also.
Topic archived. No new replies allowed.