Section 2, Lecture 32 - I think I got it

I’m learning C++ but I have a huge experience with Java, JavaScript, Perl among others, so I ended up with this:

I used a regular expression to verify if the incoming word is an isogram, and also verify if it has the same length as the hidden word. This is important to avoid “Index Out Of Bounds” exceptions (not sure how C++ calls it).

I don’t understand why we would need 2 loops, as with just one is enough to verify if the letter is the same on a particular position; as the position is given by the index of the loop.

Finally, on the main GetGuess method, I used the following to convert the input guess to lowercase (to make the life of FBullCowGame easier):

std::transform(Guess.begin(), Guess.end(), Guess.begin(), ::tolower);

I almost forgot :wink:

I think I was too fast sharing the code, as I found a problem. The logic for the bulls is right, but not for the cows. Please someone correct me if I’m wrong but I should increment Cows only if the character exist but not in the correct place right ? If so, then I think I can understand why I’d need a second loop.

Hi, I’d like to know what IDE you’re using and theme. Thanks.

Hello! I’m on a Mac, so I use XCode. The theme is called “Dusk”.

I took a similar approach here, and my code is basically the same except for the cows part. Your hunch is correct, if you make the else block like this it works:

} else { // increment cows if not
    for (int32 j = 0; j < MyHiddenWord.length(); j++) {
        if (Guess[i] == MyHiddenWord[j]) {
            BullCowCount.Cows++;
        }
    }
}

I like this way better of only running the second loop if the Bull match fails.

Privacy & Terms