Just experimenting with the code where it loops through the guess and the hidden word to determine if the guess is correct.
This code obviously gives an error when the guess is shorter than the hidden word because of the out of bound reference, but I know we don’t bother fixing that since we only pass it valid guesses.
The part that is confusing me is that it seems to error when I expect it to, except for when I pass in a guess that is 1 character shorter than the hidden word.
I fully expect the out of bounds to happen, but the code seems happy enough to run without any issues. Am I missing something obvious or is this something that c++ just does. I read something about undocumented behavior, could this be an example of that?
Here is the code I’m referring to:
int32 WordLength = MyHiddenWord.length(); // assuming same length as guess
// loop through all letters in the hidden word
for (int32 MHWChar = 0; MHWChar < WordLength; MHWChar++) {
// compare letters against the guess
for (int32 GChar = 0; GChar < WordLength; GChar++) {
// if they match then
if (Guess[GChar] == MyHiddenWord[MHWChar]) {
if (MHWChar == GChar) { // if they're in the same place
BullCowCount.Bulls++; // incriment bulls
}
else {
BullCowCount.Cows++; // must be a cow
}
}
}
}