Passing a char to a string

So you guys are probably going to yell at me for my programming lol I just finished the Bulls & Cows section and decided to play with the code as much as possible before moving on to make sure I grasp everything.

So my idea was to have every character of the hidden word showed as asterisks so let’s say the hidden word was table it would show " * * * * * ". Then instead of bulls the right character would show so if you entered tesla it would print " t * * l * " .

So this is the lines of code I came up with to test the idea but it completely crashes UE so I’m obviously doing something horribly wrong but what is it?

Blockquote
FString Pass;

for (int32 Indexz = 0; Indexz < HiddenWord.Len(); Indexz++)
{
    if (HiddenWord[Indexz] != Guess[Indexz])
    {
        Pass[Indexz] = '*';
    }
    else {
        Pass[Indexz] = HiddenWord[Indexz];
    }
}

PrintLine(TEXT("%s"), *Pass);

Blockquote

The issue you are having here is you are trying to access an index in the array ’ Pass ’ that does not exist which i believe throws an access violation and kills the program. Instead of trying to add characters to Pass by trying to set the index manually what you should do is just use the += operator

Note : Input is left over from me checking my answer It should be Guess acourding to your example

1 Like

I would suggest using this constructor
https://docs.unrealengine.com/en-US/API/Runtime/Core/Containers/FString/__ctor/8/index.html

FString Pass(HiddenWord.Len(), TEXT('*'));

This would create an FString filled with HiddenWord.Len() amount of *'s
Then you would just need to invert your condition and re/move things around.

1 Like

Thank you @Zynos_Studios this is absolutely working!

@DanM Your solution seems even more fitting as I would like to print the whole HiddenWord as asterisks on SetupGame and this seems like the better way to do it but I can’t get it working for the life of me. my first issue is it seems to not accept HiddenWord.Len() as an int32.

I tried creating an int32 variable = to HiddenWord.Len() and use that instead but it won’t work either. It seems it wants you to declare the variable inside so I must be understanding this wrong.

So what I ended up doing is declaring Pass in the header file and then initializing at setup game with

Blockquote
for (int32 Indexz = 0; Indexz < HiddenWord.Len(); Indexz++)
{
Pass += ‘*’;
}

PrintLine(TEXT("%s"), *Pass);

Blockquote

Then on input

Blockquote
for (int32 Indexz = 0; Indexz < HiddenWord.Len(); Indexz++)
{
if (HiddenWord[Indexz] == Input[Indexz])
{
Pass[Indexz] = HiddenWord[Indexz];
}
}

PrintLine(TEXT("%s"), *Pass);

Blockquote

Does that make sens? I mean it works but is it the best way to do this I’m not sure lol

**I think I haven’t quite figured out how blockquote works :joy: :joy: :joy:

I should’ve paid closer attention to the docs. It takes a string not a character, so replace the single quotes with double.

As for code blocks it’s indent by 4 spaces or surround with 3 backticks `

```
like this
```

1 Like

Ok there’s progress on this but something is still not quite right

test 2

Guess I can’t read :sweat_smile:. It will make a string from N amount of characters from another.

Figured there would be a constructor like std::string str(3, 'a');

Ok so here’s the equivalent
https://docs.unrealengine.com/en-US/API/Runtime/Core/Containers/FString/ChrN/index.html

FString Pass = FString::ChrN(HiddenWord.Len(), TEXT('*'));
1 Like

You’re a genius my man!

And with this solution Pass resets to the new HiddenWord when you reset the game as opposed to the for loop technique it would just keep adding unto the existing string lol

I believe I should clearify where I placed the code I used in my answer. It was placed in the processguess function below the check to see if the guess length was the same as the hidden word length. The variable Pass was declared inside the scope of the function so that each time the function ran the variable would be created , used to store the result of the for loop, then removed as soon as it fell out of scope. So regardless of how many times you guess or how many times you play the game the result printed to the console would be correct.

Unless you have the need to access the pass variable outside the scope of the function it’s called in I don’t see a reason why the variable shouldn’t be declared inside the function

1 Like

The problem with that unless I’m missing something is you don’t get the result when SetupGame runs. So you only get the first instance once you input something and not before.

That would be correct. I was not aware that you wanted to display the string when the get started as well. You would have to add a for loop into setup game function to make the print there as well.

FString Pass;
for ( Index = 0 ; Index < HiddenWord.Len() ; Index++ )
{
    Pass += '*';
}
PrintLine(TEXT("%s"), *Pass);

But the method used in the Assistants answer is a way nicer method to use which I hadn’t known about untill today. Glad I could learn something as well here.

1 Like

To be more efficient that should do a Reserve since you know the size to avoid potential reallocations.

FString Pass;
Pass.Reserve(HiddenWord.Len());
//...

FString uses a TArray for the storage and ChrN uses AddUninitialized which solves that issue as well.

1 Like

@Zynos_Studios Thank you still for your help as I learned of a new operator and am actually using it somewhere else in my code.

@DanM That’s amazing I was actually solving this problem with a very complicated method lmao

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

Privacy & Terms