Better way of solving problem with warning (for me)

So basically instead of putting our Guess outside of the switch, we can put this piece of code

         default:
		{
			Status = EGuessStatus::OK;
			return Guess;
                 break;
		}

in our default case :wink:

It is more understandable and simpler (i think) :smiley: Thanks for your time, and if you don’t agree please tell me why.

1 Like

I don’t see how this would not raise a warning. You still have the return statement inside the loop. Also, you shouldn’t be setting the Status like that, that’s the job of BCGame.CheckGuessValidity. The break in here isn’t doing anything since the program would reach the return statement and exit the function, you can see this by adding a breakpoint there and stepping through the code one line at a time.

What the computer sees is that there is a loop and inside that loop there is a switch statement. Only one of the cases (the default) has a return, the other’s don’t and if the while condition isn’t true, it would have reached the end of the function without ever calling a return. We as humans know that that scenario is not possible but the compiler doesn’t.

I find Ben’s logic much simpler to follow for any average programmer:

FText Guess = "";
Status = Invalid staus;
do
    Guess = user input
    Status = BCGame.CheckGuessValidity
    if the Guess is wrong, tell the player why and loop again.
    if the Guess is OK, exit the loop (the Guess is valid)
return the valid Guess

In the end, it doesn’t matter what happens inside the loop since the last line of the function is a return Guess; so as far as the compiler is concerned, it will always return something.

1 Like

Hello @Patryk_Zapasowe_Wiet,

every code after a break or an return statement will be ignored. Hence, the break will never be executed and be deleted.

Kind regards
Kevin

Privacy & Terms