Bulls and Cows Lecture 67 Similar code doesnt give the same result

Hey guys i got a question about the 67th lecture in the Bulls and Cows Section of the C++ learning course for Unreal Engine.
i’ have followed the lecture and i have almost everything exactly the same, the only difference in the IsIsogram() function of mine is that i declared the initialization of the for loop differently , ill provide the code below as well as screenshots of the desired output (the way the instructor did it) and the output i get.

some elaboration about the code, the commented bit, which is also a FOR loop ,is a code i had to copy from someone to compare it to mine i couldn’t and still cant find out whats wrong. ive only declared my initialization variables differently.

here’s the code:

bool UBullCowCartridge::IsIsogram(FString Word)const  //d|i|r|e|c|t|o|r 
{   
            //check first letter against the rest
        int32 Index = 0;
        int32 Comparison = Index +1;
        
    for(Index; Index < Word.Len(); Index++)
        { 
            for(Comparison;Comparison < Word.Len(); Comparison++)
                {
                    if (Word[Index] == Word[Comparison])
                    {
                        return false;
                    }
                }

        }


    // for (int32 Index = 0; Index < Word.Len(); Index++)
    // {
    //     for (int32 Comparison = Index +1; Comparison < Word.Len(); Comparison++)
    //     {
    //         if (Word[Index] == Word[Comparison])
    //         {
    //             return false;
    //         }
    //     }
    // }

    return true;
}

i had assumed they would be the same , here are the screenshots:

My Output:
myoutput.PNG

and this is the output i want ( the commented out code gives this output):

any help or explanation on whats happening is greatly appreciated , Thanks in advance

int32 Index = 0;
int32 Comparison = Index +1;

You’re doing this before the loop so this is done once. With a 4 letter word your loop would go

0, 1
0, 2
0, 3
1, 4 <- fails Comparison < Word.Len()

You should declare both variables in the loops. That way on the second iteration would correctly go

0, 1
0, 2
0, 3
1, 2
1, 3
2, 3

Thank you so much! Understood

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

Privacy & Terms