-
nested for loop
my code is supposed to find 1 match and break but it keeps going and finds them all. it searches my 3 words in my string array and makes sure that they are not already replaced.
Code:
for (int i=0; i < m; i++){
for (int x = 0; x <= 2; x++){
if (curWord[0][x][i] != letterguess){
if (xword[0][x][i] == letterguess){
curWord[0][x][i] = letterguess;
cout << xword[0][x] << endl;
xcount = 0;
break;
}
}
}
}
-
The break only breaks out of the inner for loop. If you want to jump out of the second one as well you need to do something else.
goto is one option.
But what I prefer is to put both loops inside of another function and return from it.
-
if i add another break then it breaks out of my do loop that runs the whole code and ends the game. should i put it in its own class?
-
Do not use goto. It is horrible to maintain if used too much, and is not good practice.
You could do something simple. Add a bool flag that is initially false, then in the inner loop set it to true, and at the end of the outter loop check if it's true. If it's true, break the outter loop as well.