While Loop always True

Hello,

I’ve just finished the lecture on switch statements. I"m having trouble with the Try # incrementing when I type in a valid guess.

Also, when I type in a valid guess, the number of Bulls and Cows is not displayed. So, the BCGame.SubmitValidGuess() function within PlayGame is not being executed.

I added a watch to my while loop and I see that it is always showing up as “true” but I don’t know what is wrong with my CheckGuessValidity() function. I’ve pasted the code here and included a screenshot of what I’m seeing when I run the program without debugging.

Any help would be much appreicated!

Thanks,
-Ryan

EGuessStatus FBullCowGame::CheckGuessValidity(FString Guess)
{

if (Guess.length() != GetHiddenWordLength())
{
	return EGuessStatus::Wrong_Wordlength;
}

else 
{
	return EGuessStatus::OK;
}

}

FText GetValidGuess()
{
//Asking the user to input a guess.
EGuessStatus Status = EGuessStatus::Invalid_Status;
FText Guess = “”;
do {
int32 CurrentTry = BCGame.GetCurrentTry();

	std::cout << "Try:" << CurrentTry;
	std::cout << ". Enter your guess:";
	getline(std::cin, Guess);
	

	EGuessStatus Status = BCGame.CheckGuessValidity(Guess);
	switch (Status)
	{
	case EGuessStatus::Wrong_Wordlength:
		std::cout << "Please enter a " << BCGame.GetHiddenWordLength() << " letter word.\n";
		break;
	case EGuessStatus::OK:
		std::cout << "OK";
		break;
	case EGuessStatus::Not_Lowercase:
		std::cout << "Please enter your word with lower case letters.\n";
		break;

	default:
		
		break;
	}
	
	std::cout << std::endl;
	
} while (Status != EGuessStatus::OK);
return Guess;

}

void PlayGame()
{
BCGame.Reset();
int32 MaxTries = BCGame.GetMaxTries();

//Asking the user to input a guess and repeat back.
//TODO change for loop to a while loop.
for (int32 i = 1; i <= MaxTries; i++) {
	FText Guess = GetValidGuess(); //TODO check for valid guess as well.

	FBullCowCount BullCowCount = BCGame.SubmitValidGuess(Guess);
	std::cout << "Bulls:" << BullCowCount.Bulls << ". Cows:" << BullCowCount.Cows << std::endl;
	std::cout << "Your guess was: " << Guess << std::endl;
	std::cout << std::endl;
}

//TODO summarize the game.
//state number of bulls and cows
//state win or loss

}

FText GetValidGuess()
{
    EGuessStatus Status = EGuessStatus::Invalid_Status;
    //code
    do 
    {
        EGuessStatus Status = BCGame.CheckGuessValidity(Guess);
        //code
    } while (Status != EGuessStatus::OK);
}

Here’s the part with the problem, you are making another Status variable within the scope of the loop which can’t also be used in the condition of the loop (as it will be out of scope). The one that IS being used for the condition (the one declared outside of the loop) is never being changed.

Thanks Dan!

That solved my issue and it makes sense.

Thanks for the explanation and the help!

Privacy & Terms