for loop pause

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.

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
             do
            {
                cout << wordAsBlanks <<endl<<endl;
                cout<<"Choose Wisely";
                char letter = reader.readChar();
                letter = tolower(letter);
                for (int i=0; i<startWord.length(); i++)
                {

                    if (letter != startWord[i])
                    {
                        userLifes = userLifes - 1;
                        cout<<"You have "<<userLifes<<" left";

                        break;
                    }
                    
                    if(startWord[i] == letter)
                    {
                        wordAsBlanks[i] = letter;

                    }
                    if(userLifes == 1)
                    {
                        cout<<dead;
                        break;
                    }

                }

            }
            while (wordAsBlanks != startWord && userLifes != 0);
            if (wordAsBlanks == startWord)
            {
                cout<<notDead;
            }
        }
        break;

Any advice?? Thanks in advance!
Last edited on
@Steven Menzies

This may not work exactly as written, but it should be pretty close.

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
 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;
Last edited on
Awsome man thanks. Just had to make like one tweak now it's working great.
Thanks again :)
@Steven Menzies

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.
Topic archived. No new replies allowed.