Lesson 32 If Statements different approach

Hello I chose a different statement, see underneath. Personally I prefer my approach to the If statement in an If statement, just wondering if there are specific reasons not to do this like I did. (Ow and don’t mind the variables. I use camelCase)
for (int32 currentGuess = 0; currentGuess < lengthHidden; currentGuess ++)
{
bool match = guess[currentGuess ] ==myHiddenWord[currentLetter];
if(match && currentLetter == i)
{
bcc.bulls++;
}
else if(match)
{
bcc.cows++;
}
else
{
}

	}
for (int32 currentGuess = 0; currentGuess < lengthHidden; currentGuess ++)
{
    bool match = guess[currentGuess ] ==myHiddenWord[currentLetter];
    if(match && currentLetter == i)
    {
        bcc.bulls++;
    }
    else if(match)
    {
        bcc.cows++;
    }
    else
    {
    }
}

Hello there, just posting this to make it a little clearer. I’ll look at you version once I’ve posted this up. I also got a different answer for this lecture.

Edit: decided to comment up here. Hello, I solved mine in a very similar way to yours. However I had a lot of issues with letters in the wrong place causing it to error. Have you tried plante or plaent for example. If they work well done you. The only criticism I would give is you’ve had to make a variable “match”. There is nothing wrong with this but it is one more thing you’d have to look at if you found a bug.

Hope that helps

Oke, thanks! I am used to putting logic in one place, but I guess in coding for games, this can easily prove to cause performance issues!

Try

 if (guess[currentGuess] == myHiddenWord[currentLetter])
 {
      if (currentLetter==currentGuess)
      {
           bcc.bulls++;
      } else
      {
            bcc.cows++;
      }
   }

(I’m assuming that in your example, i really should have been currentGuess). Storing the bool match and then testing match is less efficient than simply testing the comparison in the if statement. In your example, there are a total of four switches… A) is match true? B) is currentLetter==currentGuess? C) is A && B true? That’s just for the Bulls++… then, you asked it, if that’s false D) is match true? Technically, it’s 5 comparisons, because we had to solve match.
Using the code above…, A) do the letters match? if it’s false, we’re done, one comparison. B) is currentLetter==currentGuess?.. if it’s true, bulls++, if it’s false, cows ++. Two comparisons instead of four (five?).

Sorry for the late reply.Thanks for the response! I am learning a lot about C++

Privacy & Terms