I did it a little differently and it works just as well.
So in the header file I declared
void GetBullCows(const FString& Guess, FBullCowCount& Count) const;
Then in the ProcessGuess function I wrote:
FBullCowCount Count;
GetBullCows(Guess, Count);
PrintLine(TEXT("You have %i Bulls and %i Cows"), Count.Bulls, Count.Cows);
And finally here is the GetBullCows function:
void UBullCowCartridge::GetBullCows(const FString& Guess, FBullCowCount& Count) const
{
for (int32 GuessIndex = 0; GuessIndex < Guess.Len(); GuessIndex++)
{
if (Guess[GuessIndex] == HiddenWord[GuessIndex])
{
Count.Bulls++;
continue;
}
for (int32 HiddenIndex = 0; HiddenIndex < HiddenWord.Len(); HiddenIndex++)
{
if (Guess[GuessIndex] == HiddenWord[HiddenIndex])
{
Count.Cows++;
break;
}
}
}
}
I’m a beginner at C++ so i’m wondering if writing the code this way that I did is discouraged. Since I’m a noob I didn’t really think about that you can put the class (the struct) before you write the UBullCowCartridge class to write the function.
I thought about what would be most logical from the way we’ve done things before. So it made the most sense to me to just pass the struct by reference (since its integer values are going to change) and then declare the FBullCowCount as Count in the ProcessGuess function and send that to the GetBullCows function and then the only thing I would have to change in that code is the incrementation of the bulls and cows. To me this is more intuitive than the way the teacher did it because then I don’t have to return Count back to the ProcessGuess function and I don’t have to instance the struct again as Score and set it equal to the GetBullsCow(Guess).
However, since I am very much a beginner of not just C++ but programming in general and the teacher is a professional and knows his stuff I wonder if the way he does it is the preferred or “correct” way, and why in that case?