Short and simple IsLowerCase()

bool FBullCowGame::IsLowerCase(FString TheGuess) const
{
    for (auto Letter : TheGuess){return islower(Letter);}
}

Probly not a good idea though. the compiler shows warning c4715

Hi Geordan,
This is not a correct implementation. Have you tested your function? I’d personally create a new VS project type this single function and test it in the main.cpp:

bool IsLowerCase(std::string str) // Not the right implementation but a nice try 
{
  for (auto letter: str) { return islower(letter); }
}

int main()
{
  std::cout << IsLowerCase("ABcd") << std::endl;
  std::cout << IsLowerCase("ABCD") << std::endl;
  std::cout << IsLowerCase("abcd") << std::endl;
}
1 Like

In this case, you can think of return as a break statement. As soon as isLower() function is done checking if the first letter of TheGuess string is lower or not, the result is returned to the function that called IsLowerCase(). THerefore, once the first call to isLower() is done, it breaks out of the loops and returns the result. It only checked the very first letter of the FString TheGuess. On the plus side, you are super close to getting this function to work correctly.

1 Like

Yes i realized this almost right away. Kind of a DUH!! moment… Anyway after some more reading, watching the video a few times and coming back after a day i came to a solution

bool FBullCowGame::IsLowerCase(FString TheGuess) const//Checks the guess for CAPITALS
{
    bool b_Lower_Case = false;
    for (auto Letter : TheGuess)
    {
	    if (!islower(Letter)) { b_Lower_Case = false; }
	    else { b_Lower_Case = true; }
    }
    return b_Lower_Case;
}

I do think it goes without saying i constantly check my code, but I’m still learning, so its harder for see what you’re seeing.
Maps in general have been considerably more difficult to understand, but I think i’ve got a basic understanding now.

I appreciate the help

Thank you for the explanation. I already came to a solution, however it did not occur to me that calling return inside the loop exits the loop. Now i understand why my original attempt wasn’t working as intended.

This is better, but still not quite right :slight_smile: If you try the word “ABCd” it will wrongly consider it in lower case and return true at the end. You’re very close here, just a hint - whenever the for loop sees an upper case character (i.e. !islower(Letter)) it can freely break the for loop and return false.

bool FBullCowGame::IsLowerCase(FString TheGuess) const//Checks the guess for CAPITALS
{
    bool b_Lower_Case = true;
    for (auto Letter : TheGuess)
    {
	    if (!islower(Letter)) { b_Lower_Case = false; }
    }
    return b_Lower_Case;
}

So is the bug in the else statement? If so im just not seeing it. :confused: But this here does work properly now. planE …etc… will no longer work.

Thanks again for pointing out that bug.

Ok so, if i understand correctly,

else{b_Lower_Case = true}

will assign true if ANY character is lowercase, and this is why it wasnt working properly

This is correct! Congrats.
Just not so efficient - you can break the for loop as soon as you set b_lower_case = false; as there is already no need to continue testing the remaining letters.

A minor thing - you can skip using the b_lower_case variable at all for better readability of the code (but you can keep your version with b_lower_case, this is just telling so that you know)

bool FBullCowGame::IsLowerCase(FString TheGuess) const//Checks the guess for CAPITALS
{
    for (auto Letter : TheGuess)
    {
	    if (!islower(Letter)) { return false; }
    }
    return true;
}
1 Like

Many Thanks!!!

Privacy & Terms