Stuck in the do while error checking loop! Lesson 44,45,46

Expectation: To have the do while loop end upon EGuessStatus Status = OK.

Reality: Pictures depict the stages of the debugger. On an entry that is the correct word length.

Breakpoints

  1. EGuessStatus Status = Invalid_Status.
  2. EGuessStatus Status = OK.
  3. EGuessStatus Status = Invalid_Status

Implications: No valid guess leaves the loop. there is no return.

Workarounds: The code works fine if return is put in case EGuessStatus::OK.

Thank you for your time!

FText GetValidGuess() //loop continually til user gets valid guess
{ 
	EGuessStatus Status = EGuessStatus::Invald_Status; //Breakpoint 1 Status=Invalid_Status
	FText Guess = "";
	do
	{

		//Get guess from player
		int32 CurrentTry = BCGame.GetCurrentTry();

		std::cout << "Try " << CurrentTry << ". Enter your guess:";

		std::getline(std::cin, Guess);


		EGuessStatus Status = BCGame.CheckGuessValidity(Guess);
		switch (Status)  // Break point 2 Status = OK
		{
		case EGuessStatus::Wrong_Length:
			std::cout << "Please enter a " << BCGame.GetHiddenWordLength() << " Letter word.\n";
			break;
		case EGuessStatus::Not_IsoGram:
			std::cout << "Please enter an isogram.\n";
			break;
		case EGuessStatus::Not_Lowercase:
			std::cout << "Please use lowercase letters.\n";
			break;
		case EGuessStatus::OK:
			std::cout << "valid guess";
			break;  //if return is here code works.
		}	
		std::cout << std::endl;
	} 	

		while (Status != EGuessStatus::OK); // Break point 3 status reverts to Invalid_status and fails
		return Guess;

}

I found the error looking through other people’s source code.

Should be.

Status = BCGame.CheckGuessValidity(Guess);

Could someone explain to me the difference? I’m not sure why it would matter.

The first is creating a new variable while the latter is assigning a new value to the one previously declared.

1 Like

To clarify on @DanM’s answer, you created a second variable in your do scope. This one is separate from the one in the function body scope. So when your while is checking the variable, it actually checks the one in your function scope, instead of the one in the do scope.
By removing the EGuessState declaration, you’re actually actively reusing the variable in your function body scope, instead of declaring another variable. That is the reason it started working when you removed the enumeration type.

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

Privacy & Terms