GetBullCows function Crash

My BullCow game is crashing due to some problem with my GetBullCows function. The compilation works fine, however, when I enter a word into the terminal and press enter, the program crashes.
When I comment out the function call, the game runs without any problem.

This is my function defintion

void UBullCowCartridge::GetBullCows(const FString& PGuess, int32& BullCount, int32& CowCount) const {
    BullCount = 0;
    CowCount = 0;

    for (std::size_t k{}; k < PGuess.Len(); ++k){
        if (PGuess[k] == HiddenWord_[k]) {
            BullCount += 1;
        } else {
            for (std::size_t i{}; i < HiddenWord_.Len(); ++k){
                if (PGuess[k] == HiddenWord_[i]){
                    CowCount += 1;

This is my function call inside of ProcessGuess function

    int32 Bulls{0}, Cows{0};
    GetBullCows(Guess, Bulls, Cows);
    PrintLine(TEXT("There are %i Bulls and %i Cows"), Bulls, Cows);

This is the crash message from Unreal.

I suspect you are calling that function when the guess and hidden word are not the same length.

I found my issue… I incremented k when I should have been incrementing “i” in my second for-loop. I guess this is why I should be using appropriate names for my indexing!

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms