Cannot pass a string from an array in a struct into a PrintLine function

Hi, please help me with this problem I have come across.

I have finished the Bull Cow Game course but am now trying to add my own functions. I am working on one where it shows your stats if you decide to end the game. I get these errors:

C:\Program Files\Epic Games\UE_4.26\Engine\Source\Runtime\Core\Public\Containers/UnrealString.h(1552) : error C2338: Invalid argument(s) passed to FString::Printf
(I think this is for line 175)

C:\Program Files\Epic Games\UE_4.26\Engine\Source\Runtime\Core\Public\Containers/UnrealString.h(1554) : error C4840: non-portable use of class ‘FString’ as an argument to a variadic function
(I think this is for line 184)

Here is my cpp file

// Fill out your copyright notice in the Description page of Project Settings.
#include "BullCowCartridge.h"
#include "HiddenWordList.h"

void UBullCowCartridge::BeginPlay() // When the game starts
{
    Super::BeginPlay();
    Isograms = GetValidWords(Words);
    SetupGame(); // Setting up the game
} 

void UBullCowCartridge::OnInput(const FString& PlayerInput) // When the player hits enter
{
    // if game is over then do Clearscreen() and SetupGame()
    if (PlayerInput == "EndGame")
    {
        EndGame();
    }
    if (bRoundOver)
    {
        ClearScreen();
        SetupGame();
    }
    else // checking the player's guess
    {
        ProcessGuess(PlayerInput);
    }
}

void UBullCowCartridge::SetupGame()
{
    HiddenWord = Isograms[FMath::RandRange(0, Isograms.Num() - 1)];
    WordLength = HiddenWord.Len();
    Lives = WordLength * 2;
    bRoundOver = false;

    PrintLine(TEXT("Welcome to Bull Cows game!"));
    PrintLine(TEXT("Guess the %i letter word!"), WordLength); 
    PrintLine(TEXT("You have %i lives."), Lives);   
    PrintLine(TEXT("Type in your guess and \npress enter to continue...")); // Prompt player for guess
}

void UBullCowCartridge::EndRound()
{
    bRoundOver = true;
    PrintLine(TEXT("\nType EndGame then press enter to end the \ngame and see your stats."));
    PrintLine(TEXT("Press enter to play again..."));
}

void UBullCowCartridge::ProcessGuess(const FString& Guess)
{
    FEndScreen End;
    if (Guess == HiddenWord)
    {
        PrintLine(TEXT("You have won!"));
        EndRound();
        End.Correct++;
        End.CorrectWords.Emplace(HiddenWord);
        return;
    } 

    // Promt to guess again
    // Check if it is the right number of characters
    if (Guess.Len() != WordLength)
    {
        PrintLine(TEXT("The hidden word is %i letters long"), WordLength);
        PrintLine(TEXT("Sorry, try guessing again! \nYou have %i lives ramaining!"), Lives);
        return;
    }
    
    // Check if it is an isogram
    if (!IsIsogram(Guess)) 
    { 
        PrintLine(TEXT("No repeating letters, guess again!"));
        return;
    }

    // Remove life
    PrintLine(TEXT("Lost a life!"));
    --Lives;

    // Check if the lives are more then 0
    if (Lives <= 0)
    {
        ClearScreen();
        PrintLine(TEXT("You have no lives left"));
        PrintLine(TEXT("The hidden word was: %s"), *HiddenWord);
        End.Incorrect++;
        End.IncorrectWords.Emplace(HiddenWord);
        EndRound();  
        return; 
    }

    //Show player the bulls and the cows
    FBullCowCount Score = GetBullCows(Guess);
    PrintLine(TEXT("You have %i Bulls and %i Cows."), Score.Bulls, Score.Cows);
    PrintLine(TEXT("Guess again, you have %i lives left."), Lives);
}

bool UBullCowCartridge::IsIsogram(const FString& Word) const
{
    for (int32 Index = 0; Index < Word.Len() - 1; Index++)
    {
        for (int32 Comparison = Index + 1; Comparison < Word.Len(); Comparison++)
        {
            if (Word[Index] == Word[Comparison])
            {
                return false;
            }
        }
    }
    return true;
}

TArray<FString> UBullCowCartridge::GetValidWords(const TArray<FString>& Array) const
{
    TArray<FString> Temp;
    
    for (FString Word : Array)
    {
        if (Word.Len() >= 4 && Word.Len() <= 7 && IsIsogram(Word))
        {
            Temp.Emplace(Word);
        }
    }
    return Temp;
}

FBullCowCount UBullCowCartridge::GetBullCows(const FString& Guess) const
{
    FBullCowCount Count;

    for (int32 GuessIndex = 0; GuessIndex < Guess.Len(); GuessIndex++)
    {
        if (Guess[GuessIndex] == HiddenWord[GuessIndex])
        {
            Count.Bulls++;
            continue;
        }
        for (int32 HiddenWordIndex = 0; HiddenWordIndex < Guess.Len(); HiddenWordIndex++)
        {
            if (Guess[GuessIndex] == HiddenWord[HiddenWordIndex])
            {
                Count.Cows++;
                break; 
            }
        }
    }
    return Count;
}

void UBullCowCartridge::EndGame()
{
    ClearScreen();

    FEndScreen End;
    int32 Total = End.Correct + End.Incorrect;
    float_t Percent;
    if (Total == 0)
    {
        Percent = 0;
    }
    else
    {
        Percent = End.Correct / Total * 100;
    }

    PrintLine(TEXT("You got %i out of %i, or %f %"), End.Correct, End.Incorrect, Percent);
    
    PrintLine(TEXT("You got i% words correct."), End.Correct);
    if (End.Correct <= 1)
    {
        for (int i; i < End.Correct; i++)
        {
            PrintLine(TEXT("%s, "), End.CorrectWords[i]);
        }
    }

    PrintLine(TEXT("You got i% words incorrect"), End.Incorrect);
    if (End.Incorrect <= 1)
    {
        for (int i; i < End.Incorrect; i++)
        {
            PrintLine(TEXT("%s, "), End.IncorrectWords[i]);
        }
    }
}

And here is my header file

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "Console/Cartridge.h"
#include "BullCowCartridge.generated.h"

struct FBullCowCount
{
	int32 Bulls = 0;
	int32 Cows = 0;
};
struct FEndScreen
{
	int32 Correct = 0;
	int32 Incorrect = 0;
	TArray<FString> CorrectWords;
	TArray <FString> IncorrectWords;
};

UCLASS(ClassGroup=(Custom), meta=(BlueprintSpawnableComponent))
class BULLCOWGAME_API UBullCowCartridge : public UCartridge
{
	GENERATED_BODY()

	public:
	virtual void BeginPlay() override;
	virtual void OnInput(const FString& Input) override;
	void SetupGame();
	void EndRound();
	void EndGame();
	void ProcessGuess(const FString& Guess);
	bool IsIsogram(const FString& Word) const;
	TArray<FString> GetValidWords(const TArray<FString>& Array) const;
	FBullCowCount GetBullCows(const FString& Guess) const;

	// Your declarations go below!
	private:
	FString HiddenWord;
	int32 Lives;
	bool bRoundOver;
	bool bGameOver;
	TArray<FString> Isograms;
	int32 WordLength;
};

I have tried making the loops like this:

for (FString Word : End.CorrectWords)
        {
            PrintLine(TEXT("%s, "), Word);
        }

and

for (FString Word : End.CorrectWords)
        {
            PrintLine(Word);
        }

but neither worker.

Try *Word instead of Word

Hello, thank you I stopped getting the errors. But now when I enter the end screen Unreal Engine crashes.

This is incorrect as you have i% instead of %i

With that said your code isn’t going to work as you keep creating new End variables that will go out of scope at the end of the function. It needs to be a member variable.

Thank you so much.

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

Privacy & Terms