do-loop help

Hey guys, I need a little help with this game I am writing for a project.
I am having an issue with the do-loop function. When the game starts, the 3rd option is to quit the game; however, it doesn't do that right away. I think the error is where I put the "while" statement or maybe the brackets. I would appreciate your wisdom.

Thank you

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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
                                                                       

                                                                     

                                                                     

                                             

// This is a text based game

#include<ctime>

#include<iostream>

using namespace std;

int main()

{

    int rules (1);

    int play (2);

    int quit (3);

    int chose ;

    char again ;	



    cout << "Greetings warrior, you are about to embark in this grand chanllege\nbecause it is your destiny.\nYou are about to be tested, to become an icon, a hero, a GOD\nOr die as a peasant and be forgotten as dust in the wind.\nWelcome to...\n";

{

do{





    cout <<  "                |||||||||||||| ||||   |||| ||||||||| \n";

    cout <<  "                |||||||||||||| ||||   |||| ||||||||| \n";

    cout <<  "                     ||||      ||||   |||| ||||      \n";

    cout <<  "                     ||||      ||||||||||| ||||||||| \n";

    cout <<  "                     ||||      ||||||||||| ||||||||| \n";

    cout <<  "                     ||||      ||||   |||| ||||      \n";

    cout <<  "                     ||||      ||||   |||| ||||||||| \n";

    cout <<  "                     ||||      ||||   |||| ||||||||| \n";

    cout <<  "    ////\\\\\\\\    |||||||||\\\\\\\\   |||||||| |||\\\\\\\\       ||||    ////\\\\\\\\ "<< endl ;

    cout <<  "   ////  \\\\\\\\   ||||||||||\\\\\\\\  |||||||| ||||\\\\\\\\      ||||   ////  \\\\\\\\  "<< endl ;

    cout <<  "  ////    \\\\\\\\  ||||       \\\\\\\\ ||||     |||| \\\\\\\\     ||||  ////    \\\\\\\\ "<< endl ;

    cout <<  " ||||      |||| ||||       //// |||||||| ||||  \\\\\\\\    |||| ||||      ||||"<< endl ;

    cout <<  " |||||||||||||| ||||||||||////  |||||||| ||||   \\\\\\\\   |||| ||||||||||||||"<< endl ;

    cout <<  " |||||||||||||| |||||||||||||   ||||     ||||    \\\\\\\\  |||| ||||||||||||||"<< endl ;

    cout <<  " ||||      |||| ||||     ||||   |||||||| ||||     \\\\\\\\ |||| ||||      ||||"<< endl ;

    cout <<  " ||||      |||| ||||     ||||   |||||||| ||||      \\\\\\\\|||| ||||      ||||"<< endl ;

	  	

	 cout << "\n1:Rules\n2:Play\n3:Quit\n";



         cin >> chose ;



     {

        if (chose == 1) // Rules

            {

                cout << "\nThe Rules for the arena are simple. You must defeat your oponent.\nInput the number that represents your choice.\n1:You must pick a Race.\n2:You must select a Weapon.\nOnce a choice is made the Battle will beggin.\nBy random computation your score will be display.\nAnd dont forget that in the arena there can be only one champion!!!\n";

            }

        else if (chose == 2) // Race

            {

            int race;

            int human (1);

            int orc (2) ;

            int elf (3) ;

                cout << "\nYou are about to enter the arena.\nBut first" << endl;

                cout << "select your race.\n";

                cout << "1:Human\n2:Orc\n3:Elf\n";

                cin >> race ;



                if (race == 1) // Human

                {

                    cout << "\nSo a human?\nYour courage is un matched, a great warrior you make everyone think twice.\nYou fight with intelligence and heart.\n";

                    human = (100);

                }

                else if (race == 2) //Orc



                {

                    cout << "\nSo you are a mighty Orc?\nYou are a fierce warrior. Everyone fear you.Who ever stand against you doesnt last long.\nHonor or Death!\n " ;

                    orc = (200);

                }

                else if (race == 3) // Elf

                {

                    cout << "\nAn elf?\nFull of wisdowm, you have been in many battles and won them all.\n";

                    elf = (300);

                }



                    {

                        cout << "\nPlease select your Mystical Weapon.\nOnce a wepon is chosen the battle will begging."<< endl;

                        cout << "1:Flemish Sword\n2:Bow & Arrow of the Acients\n3:Magic of the old ones\n";



                        int sword (1);

                        int bow (2);

                        int magic (3);

                        int weapon ;

			int score;

                        cin >> weapon ;

			

			int random;

			srand(time(NULL));

                            if (weapon == 1 || weapon == 2 )

                            {

                              	random = rand() % 4 +1;

				cout << random << endl;

				

				if (random == 1 )

				

				{	   

					score = score + 100;

					cout << "You hit your target! you have a final score of " << score << "." << endl;

				}



				if (random == 2 )

				

				{	   

					score = score + 200;

					cout << "You hit your target! your final score is " << score << "." << endl;

				}

                            

				if (random == 3 )

				

				{	   

					score = score + 300;

					cout << "Critical hit, your final score is " << score << "." << endl;

				}

				

				if (random == 4 )

				

				{	   

					score = 0;

					cout << "you missed your taget! you score is " << score << "." << endl;

				}







			}





                            else if (weapon == 3)

                            {

                                random = rand () % 2 +1;

				cout << random << endl;

				

				if (random == 1) 

				{

					score = score + 200;

					cout << "You where successful, " << score << " is awarded." << endl;

				}

		

				if (random == 2) 

				{

					score = score - 100;

					cout << "You fail! " << score << " is deducted." << endl; 

				}



                            }











                    }





		



          		 }

		cout << "\nWould you like to return to the main menu (y/n)? ";

		cin >> again;

			}	

			}while ( again == 'y' || again == 'Y' );







    }

if (chose == 3) // Quit

   {

     cout << "you quit the game.\n";

   }





   return 0;

 

}

closed account (Dy7SLyTq)
a) its called a do-while loop
b) its not a function
c) whats up with the white space?
d) could you make it in a nicer format so we can read it easier?
e) why not use an enum instead of taking up space with unused variables?
Yeah, sorry about that. This is only my 4th week learning c++, So I am still in the basic. I was writing it on CentOs, but I understand I left many big gaps in the program. I haven't learned about enum yet. :/ I will fix it up to the best I can, then repost it again.
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
                                                                                                                                
                                                               
// By Christopher Alfonso 
//cop1334c JC                                   
// This is a text based game

#include<ctime>
#include<iostream>
using namespace std;
int main()
{
    int rules (1);
    int play (2);
    int quit (3);
    int chose ;
    char again ;	

    cout << "Greetings warrior, you are about to embark in this grand chanllege\nbecause it is your destiny.\nYou are about to be tested, to become an icon, a hero, a GOD\nOr die as a peasant and be forgotten as dust in the wind.\nWelcome to...\n";

{
do{
    cout <<  "                |||||||||||||| ||||   |||| ||||||||| \n";
    cout <<  "                |||||||||||||| ||||   |||| ||||||||| \n";
    cout <<  "                     ||||      ||||   |||| ||||      \n";
    cout <<  "                     ||||      ||||||||||| ||||||||| \n";
    cout <<  "                     ||||      ||||||||||| ||||||||| \n";
    cout <<  "                     ||||      ||||   |||| ||||      \n";
    cout <<  "                     ||||      ||||   |||| ||||||||| \n";
    cout <<  "                     ||||      ||||   |||| ||||||||| \n";
    cout <<  "    ////\\\\\\\\    |||||||||\\\\\\\\   |||||||| |||\\\\\\\\       ||||    ////\\\\\\\\ "<< endl ;
    cout <<  "   ////  \\\\\\\\   ||||||||||\\\\\\\\  |||||||| ||||\\\\\\\\      ||||   ////  \\\\\\\\  "<< endl ;
    cout <<  "  ////    \\\\\\\\  ||||       \\\\\\\\ ||||     |||| \\\\\\\\     ||||  ////    \\\\\\\\ "<< endl ;
    cout <<  " ||||      |||| ||||       //// |||||||| ||||  \\\\\\\\    |||| ||||      ||||"<< endl ;
    cout <<  " |||||||||||||| ||||||||||////  |||||||| ||||   \\\\\\\\   |||| ||||||||||||||"<< endl ;
    cout <<  " |||||||||||||| |||||||||||||   ||||     ||||    \\\\\\\\  |||| ||||||||||||||"<< endl ;
    cout <<  " ||||      |||| ||||     ||||   |||||||| ||||     \\\\\\\\ |||| ||||      ||||"<< endl ;
    cout <<  " ||||      |||| ||||     ||||   |||||||| ||||      \\\\\\\\|||| ||||      ||||"<< endl ;
 	
	 cout << "\n1:Rules\n2:Play\n3:Quit\n";
         cin >> chose ;
     {
        if (chose == 1) // Rules
            {
                cout << "\nThe Rules for the arena are simple. You must defeat your oponent.\nInput the number that represents your choice.\n1:You must pick a Race.\n2:You must select a Weapon.\nOnce a choice is made the Battle will beggin.\nBy random computation your score will be display.\nAnd dont forget that in the arena there can be only one champion!!!\n";
            }

        else if (chose == 2) // Race
            {
            int race;
            int human (1);
            int orc (2) ;
            int elf (3) ;

                cout << "\nYou are about to enter the arena.\nBut first" << endl;
                cout << "select your race.\n";
                cout << "1:Human\n2:Orc\n3:Elf\n";

                cin >> race ;

                if (race == 1) // Human
                {
                    cout << "\nSo a human?\nYour courage is un matched, a great warrior you make everyone think twice.\nYou fight with intelligence and heart.\n";

                    human = (100);
                }
                else if (race == 2) //Orc
                {
                    cout << "\nSo you are a mighty Orc?\nYou are a fierce warrior. Everyone fear you.Who ever stand against you doesnt last long.\nHonor or Death!\n " ;

                    orc = (200);
                }
                else if (race == 3) // Elf
                {
                    cout << "\nAn elf?\nFull of wisdowm, you have been in many battles and won them all.\n";
 
                   elf = (300);
                }

                    {
                        cout << "\nPlease select your Mystical Weapon.\nOnce a wepon is chosen the battle will begging."<< endl;

                        cout << "1:Flemish Sword\n2:Bow & Arrow of the Acients\n3:Magic of the old ones\n";

                        int sword (1);
                        int bow (2);
                        int magic (3);
                        int weapon ;
			int score;

                        cin >> weapon ;

			int random;
			srand(time(NULL));// Weapon 1 & 2 random sequence

                            if (weapon == 1 || weapon == 2 )
                            {
                              	random = rand() % 4 +1;
				cout << random << endl;

				if (random == 1 )
				{	   
					score = score + 100;
					cout << "You hit your target! you have a final score of " << score << "." << endl;
				}

				if (random == 2 )
				{	   
					score = score + 200;
					cout << "You hit your target! your final score is " << score << "." << endl;
				}
                           
				if (random == 3 )
				{	   
					score = score + 300;
					cout << "Critical hit, your final score is " << score << "." << endl;
				}

				if (random == 4 )		
				{	   
					score = 0;
					cout << "you missed your taget! you score is " << score << "." << endl;
				}

		            }

                            else if (weapon == 3)// Weapon 3 random sequence
                            {
                                random = rand () % 2 +1;
				cout << random << endl;
			
				if (random == 1) 
				{
					score = score + 200;
					cout << "You where successful, " << score << " is awarded." << endl;
				}	

				if (random == 2) 
				{
					score = score - 100;
					cout << "You fail! " << score << " is deducted." << endl; 
				}
                       }

                    }
          		 		}   // Would you like to play again
					cout << "\nWould you like to return to the main menu (y/n)? ";
					cin >> again;
					}	
						}while ( again == 'y' || again == 'Y' );
    }
 	if (chose == 3) // Quit
   	{
        cout << "you quit the game.\n";
   	}
   return 0;
}
> a) its called a do-while loop.

not necessarily, many programmers call it the 'do loop' and some call it the 'do-while loop' both terms are correct.
Last edited on
closed account (Dy7SLyTq)
alright, but (and this is just the grammar nazi in me) it wont be do-loop
:P
ok, so any help?
Last edited on
closed account (Dy7SLyTq)
sorry i cant tell. your formatting is bad, i see at least three different styles of coding, you have brackets lying around for no reason, tons of unused and incorrectly used variables, and too much whitespace. its a mess
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

// By Christopher Alfonso
//cop1334c JC
// This is a text based game

#include<ctime>
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
    int rules (1);
    int play (2);
    int quit (3);
    int chose ;
    char again ;

    cout << "Greetings warrior, you are about to embark in this grand chanllege\nbecause it is your destiny.\nYou are about to be tested, to become an icon, a hero, a GOD\nOr die as a peasant and be forgotten as dust in the wind.\nWelcome to...\n";

do{
    cout <<  "                |||||||||||||| ||||   |||| ||||||||| \n";
    cout <<  "                |||||||||||||| ||||   |||| ||||||||| \n";
    cout <<  "                     ||||      ||||   |||| ||||      \n";
    cout <<  "                     ||||      ||||||||||| ||||||||| \n";
    cout <<  "                     ||||      ||||||||||| ||||||||| \n";
    cout <<  "                     ||||      ||||   |||| ||||      \n";
    cout <<  "                     ||||      ||||   |||| ||||||||| \n";
    cout <<  "                     ||||      ||||   |||| ||||||||| \n";
    cout <<  "    ////\\\\\\\\    |||||||||\\\\\\\\   |||||||| |||\\\\\\\\       ||||    ////\\\\\\\\ "<< endl ;
    cout <<  "   ////  \\\\\\\\   ||||||||||\\\\\\\\  |||||||| ||||\\\\\\\\      ||||   ////  \\\\\\\\  "<< endl ;
    cout <<  "  ////    \\\\\\\\  ||||       \\\\\\\\ ||||     |||| \\\\\\\\     ||||  ////    \\\\\\\\ "<< endl ;
    cout <<  " ||||      |||| ||||       //// |||||||| ||||  \\\\\\\\    |||| ||||      ||||"<< endl ;
    cout <<  " |||||||||||||| ||||||||||////  |||||||| ||||   \\\\\\\\   |||| ||||||||||||||"<< endl ;
    cout <<  " |||||||||||||| |||||||||||||   ||||     ||||    \\\\\\\\  |||| ||||||||||||||"<< endl ;
    cout <<  " ||||      |||| ||||     ||||   |||||||| ||||     \\\\\\\\ |||| ||||      ||||"<< endl ;
    cout <<  " ||||      |||| ||||     ||||   |||||||| ||||      \\\\\\\\|||| ||||      ||||"<< endl ;

	 cout << "\n1:Rules\n2:Play\n3:Quit\n";
         cin >> chose ;
     {
        if (chose == 1) // Rules
            {
                cout << "\nThe Rules for the arena are simple. You must defeat your oponent.\nInput the number that represents your choice.\n1:You must pick a Race.\n2:You must select a Weapon.\nOnce a choice is made the Battle will beggin.\nBy random computation your score will be display.\nAnd dont forget that in the arena there can be only one champion!!!\n";
            }
        if (chose == 3) // Quit
            break;
        else if (chose == 2) // Race
            {
            int race;
            int human (1);
            int orc (2) ;
            int elf (3) ;

                cout << "\nYou are about to enter the arena.\nBut first" << endl;
                cout << "select your race.\n";
                cout << "1:Human\n2:Orc\n3:Elf\n";

                cin >> race ;

                if (race == 1) // Human
                {
                    cout << "\nSo a human?\nYour courage is un matched, a great warrior you make everyone think twice.\nYou fight with intelligence and heart.\n";

                    human = (100);
                }
                else if (race == 2) //Orc
                {
                    cout << "\nSo you are a mighty Orc?\nYou are a fierce warrior. Everyone fear you.Who ever stand against you doesnt last long.\nHonor or Death!\n " ;

                    orc = (200);
                }
                else if (race == 3) // Elf
                {
                    cout << "\nAn elf?\nFull of wisdowm, you have been in many battles and won them all.\n";

                   elf = (300);
                }

                    {
                        cout << "\nPlease select your Mystical Weapon.\nOnce a wepon is chosen the battle will begging."<< endl;

                        cout << "1:Flemish Sword\n2:Bow & Arrow of the Acients\n3:Magic of the old ones\n";

                        int sword (1);
                        int bow (2);
                        int magic (3);
                        int weapon ;
			int score;

                        cin >> weapon ;

			int random;
			srand(time(NULL));// Weapon 1 & 2 random sequence

                            if (weapon == 1 || weapon == 2 )
                            {
                              	random = rand() % 4 +1;
				cout << random << endl;

				if (random == 1 )
				{
					score = score + 100;
					cout << "You hit your target! you have a final score of " << score << "." << endl;
				}

				if (random == 2 )
				{
					score = score + 200;
					cout << "You hit your target! your final score is " << score << "." << endl;
				}

				if (random == 3 )
				{
					score = score + 300;
					cout << "Critical hit, your final score is " << score << "." << endl;
				}

				if (random == 4 )
				{
					score = 0;
					cout << "you missed your taget! you score is " << score << "." << endl;
				}

		            }

                            else if (weapon == 3)// Weapon 3 random sequence
                            {
                                random = rand () % 2 +1;
				cout << random << endl;

				if (random == 1)
				{
					score = score + 200;
					cout << "You where successful, " << score << " is awarded." << endl;
				}

				if (random == 2)
				{
					score = score - 100;
					cout << "You fail! " << score << " is deducted." << endl;
				}
                       }

                    }
          		 		}   // Would you like to play again
					cout << "\nWould you like to return to the main menu (y/n)? ";
					cin >> again;
					}
						}while ( again == 'y' || again == 'Y' );

        cout << "\nyou quit the game.\n";
   return 0;
}
Topic archived. No new replies allowed.