For loops within for loops, within for loops..

I've started working on a simple text based game, not even with any intention to finish it, just to see how far I can get.
I have the player choose their Gender, Name and Race, then show their choices and ask them to confirm it or choose something to change (Name, Race, Gender, or all)

I keep all of the choices within separate for loops, incase of an invalid entry, but it's not going quite as planned.

The problem I have is that the code always stays in the "What would you like the change?" section after I've chosen something new to change.


I literally started last tuesday, no laughing!

I'm not even sure what state this will copy/paste in.
Thanks in advance, I'm just trying to understand it better haha
Josh
(Code on next post, if it lets me add it soon enough)
Before posting the code, please use code tags (see the "format" panel at the side of the "reply" box).
Shifted everything to the left to save characters from spaces, might be harder to read though
:)
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
#include<iostream>
#include<string>
using namespace std;

struct character {
int age;
string gender;
string name;
string race;
string clss;
};

void invalidEntry(void) {
cout << "Invalid entry, try again.\n";
}



int main() {
//initialize variables
string getRace;
character player;
player.race="none";
player.gender="none";
string charName = "null";
string charCorrect;
string charGender="none";
string change="none"; 

cout << "Welcome to Akalon." << endl;
cout << "What is your gender? Male, or Female.\n";
for(;player.race=="none"&&charGender=="none";) {
cin >> charGender;

if (charGender=="Male"||charGender=="male"||charGender=="m"||charGender=="M"||charGender=="MALE") {
player.gender="Male";

cout << player.gender << " selected." << endl;
}
else if (charGender=="Female"||charGender=="female"||charGender=="f"||charGender=="F"||charGender=="FEMALE") {
player.gender="Female";
cout << player.gender << endl;
}
else { 
invalidEntry();
cout << "Male, or Female?\n";
player.gender="none";
charGender="none";

}
}


cout << "What is your name, ";
if (player.gender=="Male") {cout << "boy?" << endl;}
if (player.gender=="Female") {cout << "girl?" << endl;}
cin >> player.name;
cout << "Hello, " << player.name;
cout << ". Which race are you then? " << endl;
cout << "Human\nOrc\n";

//fore loop that checks if the player has entered a correct value, if not, it loops.
for(;player.race=="none";){
//start of race for loop
//Race selection
cin >> getRace;    
if (getRace=="human" || getRace=="Human" || getRace=="h" ||getRace=="H") {

player.race="Human";
cout << "A noble choice, Human.\n";
}
else if (getRace=="orc" || getRace=="Orc" ||getRace== "o" || getRace=="O") {

player.race="Orc";
cout<< "For honor, Orc.\n";
}

else { 
invalidEntry();
cout << "Human, or Orc.\n";
}


} //end of race for loop

//check if the player is sure about the entered values
cout << "You have chosen the name " << player.name << " the gender " << player.gender << " and the race " << player.race << "." << endl;
cout << "Is this correct? y/n\n";
//another for loop to ensure correct entry
for(;charCorrect!="y"&& charCorrect!="n"&&charCorrect!="yes"&&charCorrect!="no";) {
cin >> charCorrect;


if (charCorrect=="n" || charCorrect=="no" || charCorrect=="NO" ||charCorrect=="N" && change!="changed") {

cout << "What would you like to change?\nName/Race/Gender/Everything\n";

for(;change!="name";) {
cin >> change; 
if (change=="name")
{ //start of name

for(;player.name!="null"&&change!="changed";) {   
player.name="null";                
cout << "What would you like to change your name to?" << endl;

cin >> player.name;
cout << "Very well, " << player.name << "."; 
change="changed";
charCorrect="yes";

}
} // end of name
else if (change=="race") { // start of race
player.race="none";
getRace="none";
cout << "What would you like your race to be, \nHuman, or Orc?" << endl;
for(;player.race=="none";){
//start of race for loop
//Race selection
cin >> getRace;    
if (getRace=="human" || getRace=="Human" || getRace=="h" ||getRace=="H") {

player.race="Human";
cout << "Okay, Human.\n";
charCorrect="yes";
break;
}
else if (getRace=="orc" || getRace=="Orc" ||getRace== "o" || getRace=="O") {

player.race="Orc";
cout<< "It is done, Orc.\n";
charCorrect="yes";
break;
}

else { 
invalidEntry();
cout << "Human, or Orc.\n";
}

//end of player.race=none for loop 
}


} // end of race 
else if (change=="gender") {//start of gender
player.gender="none";
charGender="none";
cout << "So what gender are you?" << endl;

for(;player.gender=="none"&&charGender=="none";) {
cin >> charGender;

if (charGender=="Male"||charGender=="male"||charGender=="m"||charGender=="M"||charGender=="MALE") {
player.gender="Male";

cout << player.gender << " selected." << endl;
charCorrect="yes";
change="changed";
break;
}
else if (charGender=="Female"||charGender=="female"||charGender=="f"||charGender=="F"||charGender=="FEMALE") {
player.gender="Female";
cout << player.gender << " selected." << endl;
charCorrect="yes";
change="changed";
break;
}
else { 
invalidEntry();
cout << "Male, or Female?";
player.gender="none";
charGender="none";
break;

}
} //end of gender for loop
} //end of gender
else if (change=="all"||change=="everything") {
player.name="null";
player.race="none";
player.gender="none";
charGender="none";
cout << "So gender would you like to be, Male or Female?";
for(;player.race=="none"&&charGender=="none";) {
cin >> charGender;

if (charGender=="Male"||charGender=="male"||charGender=="m"||charGender=="M"||charGender=="MALE") {
player.gender="Male";
cout << player.gender << " selected." << endl;


}
else if (charGender=="Female"||charGender=="female"||charGender=="f"||charGender=="F"||charGender=="FEMALE") {
player.gender="Female";
cout << player.gender << " selected." << endl;

}
else { 
invalidEntry();
cout << "Male, or Female?";
player.gender="none";
charGender="none";

}
}//end of all gender for loop


{//start of all name                  
cout << "What would you like to change your name to?" << endl;
cin >> player.name;
cout << "Very well, " << player.name << "."; 
//end of all name
}

//start of all race
cout << "What race would you like to be?";

for(;player.race=="none";){
//start of all race for loop
//Race selection
cin >> getRace;    
if (getRace=="human" || getRace=="Human" || getRace=="h" ||getRace=="H") {

player.race="Human";
cout << "A noble choice, Human.\n";
charCorrect="yes";
change="changed";
break;

}
else if (getRace=="orc" || getRace=="Orc" ||getRace== "o" || getRace=="O") {

player.race="Orc";
cout<< "For honor, Orc.\n";
charCorrect="yes";
change="changed";
break;

}

else { 
invalidEntry();
cout << "Human, or Orc.\n";
}


}//end of race for loop





} //end of everything/all
/*    else if (change!="race"&&change!="Race"&&change!="gender"&&change!="Gender"&&change!="all"&&change!="All"&&change!="everything"&&change!="Everything"&&change!="changed") {
change="none";
invalidEntry();                    
cout << "WHY ARE YOU HERE";
cout << change;
}*/


//end of y/n for loop
}
cout << "Your name is now " << player.name << ". Your Race is " << player.race << " and you are a " << player.gender << "." << endl;
break;


} //charcorrect=n close brace


else if (charCorrect=="y" || charCorrect=="yes" || charCorrect=="Y" || charCorrect=="Yes"|| charCorrect=="YES") {
cout << "Very Well." << endl;
break;
}//charcorrect=y close brace       
else {
invalidEntry();
cout << "Please choose Yes or No";
} // correct = yes close brace

}// correct = yes/no question close brace



cout << "Continuation message, you are in a forest, or something similar.";

return 0;  
} // end main brace  
Last edited on
EDIT:

You're just missing some break; points.

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
            for(; change!="name";)
            {
                cin >> change;
                if (change=="name")
                {
                    //start of name

                    for(; player.name!="null"&&change!="changed";)
                    {
                        player.name="null";
                        cout << "What would you like to change your name to?" << endl;

                        cin >> player.name;
                        cout << "Very well, " << player.name << ".";
                        change="changed";
                        charCorrect="yes";
                        break; //YOU NEED THIS BREAK

                    }
                } // end of name


                //more code


            cout << "Your name is now " << player.name << ". Your Race is " << player.race << " and you are a " << player.gender << "." << endl;
            break; //YOU NEED THIS BREAK

            } 


It's formatting got completely screwed up here when I posted it but it should help. You'll need to add breaks in the other sections such as where you change race, gender, etc.

Last edited on
Thanks I'll try and add a break on every result, I don't need breaks on else InvalidEntry()'s do I? because that's meant to loop anyway

edit: Score! thanks. I didn't even consider adding breaks at the end of parent scopes, or whatever they're called in C++ :)
Last edited on
closed account (S6k9GNh0)
If that break is needed then the purpose of a for loop is gone. I didn't review the code completely but just saying..
If that break is needed then the purpose of a for loop is gone. I didn't review the code completely but just saying..


He's right. The for loops you have setup aren't needed because by the look of the chunks they aren't designed to repeat anyway.

That's just pertaining to the code you have for changing attributes. The other for loops may or may not be necessary. I don't know what you intend.
They're meant to loop if an input isn't a valid choice, like
"Choose Race. Male or Female"
if you enter "both" or something, I don't know any other way to make it so you have to enter it again

Like I said, I'm a newbie so if there's a better way to do that I'll be happy to know as for loops are a bit tedious

:)

edit: That's why I've only put the breaks when a valid answer's given, to get out of the loop

Josh
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
for(; player.name!="null"&&change!="changed";)
                    {
                        player.name="null";
                        cout << "What would you like to change your name to?" << endl;

                        cin >> player.name;
                        cout << "Very well, " << player.name << ".";
                        change="changed";
                        charCorrect="yes";
                        break; //YOU NEED THIS BREAK

                    }


This will never repeat. That break the is required to make it work will get out of the loop regardless. Even if it wasn't there because the player is unlikely to type null (although it would make a decent character name) it will always except what they input and always make the string change turn to "changed". Even if you type null change will still be given the green flag and the for loop doesn't loop.

Here's pseudo code for what I think you are trying to accomplish:

1
2
3
4
5
6
7
8
9
10
while (player_happy_with_choice == false)
cout << "What do you want to change....?\n";
getline(cin, what_I_want_to_change); //I always use getline for strings
cout << "What do you want to change...to?\n";
getline(cin, name);
cout << "Are you happy with " << name << "? y/n" << endl;
cin >> y; 
cout << "Okay\n";
//loop ends 
//if user typed n it would repeat the query 


Is that more what you are looking for?
Topic archived. No new replies allowed.