I'm having an issue with my for loop. Basically what i'm doing is i'm making a virtual hangman game for one of my labs in school. I put a for loop to loop through the word that I put in for the user to guess. I have an else if statement inside the for loop to make a condition that they give the wrong answer. The problem i'm having is the for loop is causing it to loop for the length of the word. I tried putting in a break statement but it breaks the whole loop. Heres what I got.
do
{
bool ok = true;
cout << wordAsBlanks <<endl<<endl;
cout<<"Choose Wisely";
char letter = reader.readChar();
letter = tolower(letter);
for (int i=0; i<startWord.length(); i++) // First loop to check if letter is shown even once in startWord
{
if (letter == startWord[i])
{
ok = true;
break;
}
}
for (int i=0; i<startWord.length(); i++)// Now add the letter to wordAsBlanks as many as needed
{
if(startWord[i] == letter)
{
wordAsBlanks[i] = letter;
}
}
if (!ok) // If no letters detected, (ie ok still false), decrease userLifes
userLifes--;
if(userLifes == 0)// if userLifes = 0 .. Don't end game if player still has a life
{
cout<<dead;
break;
}
}
while (wordAsBlanks != startWord && userLifes); // Just using userLifes, means > 0. No need for
// userLifes >0
if (wordAsBlanks == startWord)
{
cout<<notDead;
}
}
break;
Yeah, I think the tweak was changing bool ok = true; to bool ok = false; in the beginning of that code. Otherwise, it never changes. Sorry 'bout that. Glad you got your program working.