Bug in Ben's IsLowerCase() and the fix

urc_s02_design_a_helper_function

#1

Hi all,

I found a bug in Ben’s IsLowerCase() implementation - bold claim, I know, but I’ve shown my working.

In Ben’s code, should the User input an uppercase character in any position other than the first then it will not return the correct error, nor will it print the error to the User.
After an embarrassing amount of head scratching I have found the solution so that the correct error does print, regardless of where the User may input an uppercase character.
Please see below but forgive the non-use of a snipping tool:

bool FBullCowGame::IsLowerCase(FString Guess) const
{
	if (Guess.length() <= 1) { return true; }

	bool bLowerStatus= true;

	for (auto Letter : Guess)
	{
		if (!islower(Letter))
		{
			bLowerStatus= false;
			break;
		}

		else
		{
			bLowerStatus= true;
		}
	}
		return bLowerStatus;
}

The “break;” (in bold) is needed. You will note that I have also set a boolean variable of “Status”, when I tried commenting out Status and return only true/false it had the same error - I would be greatly appreciative if anyone can explain why this is clearly to me as I feel like I get why but cannot properly explain it myself. I think it is because when one instructs the function to return within the “if” statements then it will not execute any further checks and will exit the whole function.
I tried having the boolean values switch around to see if the check operated the same way:

bool FBullCowGame::IsLowerCase(FString Guess) const
{
	if (Guess.length() <= 1) { return true; }

	bool bLowerStatus= false;

	for (auto Letter : Guess)
	{
		if (islower(Letter))
		{
			bLowerStatus= true;
			
		}

		else
		{
			bLowerStatus= false;
			break;
		}
	}
		return bLowerStatus;
	
}

… and found it would but only if the “break” followed the condition that the Status was found to state that “there is a non-lowercase letter in the Guess”.

Thanks for bearing with a long post, do test your code as Ben implemented it before trying out my method as I am keen to know if I got something wrong in the first place!

Sam