guessing game

Does anyone know why I only get the first letter correct? When I try to guess the letter "h" or any other correct letter, it prints out "try again" instead of "You guessed a letter"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
bool checkGuess(char answer[], char guess, int size)
{
	for (int i = 0; i < size; i++)
	{

		if (guess == answer[i])
		{
			return true;
		}
		else if (guess != answer[i])
		{

			return false;
		}
	}
}
Last edited on
closed account (E0p9LyTq)
Take a look at the logic of your checkGuess() function. You only check the first element of your answer array and return true or false. You don't check any of the other array's elements.

1
2
3
4
5
6
7
8
9
10
11
12
bool checkGuess(char answer[], char guess, int size)
{
   for (int i = 0; i < size; i++)
   {

      if (guess == answer[i])
      {
         return true;
      }
   }
   return false;
}
Is there examples that I can take a look at? Something that will lead me in the right direction. I have no idea on how to fix that; I'm so lost.
closed account (E0p9LyTq)
galaxylfc wrote:
Is there examples that I can take a look at?

Did you see my first reply to you?
I was looking at it wrong. I didn't realize you put "return false;" outside of the for loop! No wonder it still wasn't making any sense to me! Thank you so much!
closed account (E0p9LyTq)
@galaxylfc,

Not a problem, does your code work now the way you want?

Cheers!
Yes it does, thank you! I really appreciate it!
closed account (E0p9LyTq)
Now make the game end if the player guesses all the letters before the turns run out, printing out a "WINNER!" message.

Go forth and Code! ;)
That is exactly what I am working on now haha!
Topic archived. No new replies allowed.