Home > Mobile >  boolean check behaving unexpectedly when returning value
boolean check behaving unexpectedly when returning value

Time:03-15

#include <iostream> 
#include <string>

bool is_favorite(std::string word) 
{
    int isTrueCounter = 0;

    std::cout << "\nisTrueCounter: " << isTrueCounter;

    if (isTrueCounter == word.length())
    {
        return true;
    }
    else {
        for (int i = 0; i < word.length(); i  )
        {
            if (word[i] == 'a' || word[i] == 'A')
            {
                isTrueCounter  ;
                std::cout << "\nisTrue  1 ";
            }
            else
            {
                if (word[i] == 'b' || word[i] == 'B')
                {
                    isTrueCounter  ;
                    std::cout << "\nisTrue  1 ";
                }
                else
                {
                    if (word[i] == 'c' || word[i] == 'C')
                    {
                        isTrueCounter  ;
                        std::cout << "\nisTrue  1 ";
                    }
                    else {
                        if (word[i] == 'd' || word[i] == 'D')
                        {
                            isTrueCounter  ;
                            std::cout << "\nisTrue  1 ";
                        }
                        else
                        {
                            if (word[i] == 'e' || word[i] == 'E')
                            {
                                isTrueCounter  ;
                                std::cout << "\nisTrue  1 ";
                            }
                            else
                            {
                                if (word[i] == 'f' || word[i] == 'F')
                                {
                                    isTrueCounter  ;
                                    std::cout << "\nisTrue  1 ";
                                }
                            }
                        }
                    }
                }
            }

        } //for
    }

    std::cout << "\nisTrueCounter: " << isTrueCounter;

}

int main()
{
    std::string favWord;
    std::cout << "Please input your word: ";
    std::cin >> favWord;
    std::cout << "\nFavWord Length: " << favWord.length();
    
    if (is_favorite(favWord) == true)
    {
        std::cout << "\nThis is a favorite word!";
    }
    else
    {
        std::cout << "\nThis is NOT a favorite word!";
    }
}

Here is my code, I am attempting to pass a string into a boolean function that will return true if all criteria is met by the string passed. The qualifications for a "passing" word is that it contains ONLY the letters a-f (of either case) so words like AaAa or Cafe or Bad should pass, but after trial and error, even words that I know should pass are failing and I feel like I am keeping track of the letters' qualifications properly, by incrementing on a variable (isTrueCounter) to count if all the characters in the string are qualifying characters, but even when the function should be returning true, the false case displays. What am I doing wrong? What am I not seeing? When I run this code it will display the variables to help keep track of when stuff is being added to the holder variables but even when all the numbers are right the false case displays.

CodePudding user response:

You didn't return anything, you can do this in many way. For example from your code, return false immediatly after condition is not true (not in a-f).

bool is_favorite(std::string word) 
{
    int isTrueCounter = 0;
    std::cout << "\nisTrueCounter: " << isTrueCounter;
    if (isTrueCounter == word.length())
    {
        return true;
    }
    else {
        for (int i = 0; i < word.length(); i  )
        {
            if (word[i] == 'a' || word[i] == 'A')
            {
                isTrueCounter  ;
                std::cout << "\nisTrue  1 ";
            }
            else
            {
                if (word[i] == 'b' || word[i] == 'B')
                {
                    isTrueCounter  ;
                    std::cout << "\nisTrue  1 ";
                }
                else
                {
                    if (word[i] == 'c' || word[i] == 'C')
                    {
                        isTrueCounter  ;
                        std::cout << "\nisTrue  1 ";
                    }
                    else {
                        if (word[i] == 'd' || word[i] == 'D')
                        {
                            isTrueCounter  ;
                            std::cout << "\nisTrue  1 ";
                        }
                        else
                        {
                            if (word[i] == 'e' || word[i] == 'E')
                            {
                                isTrueCounter  ;
                                std::cout << "\nisTrue  1 ";
                            }
                            else
                            {
                                if (word[i] == 'f' || word[i] == 'F')
                                {
                                    isTrueCounter  ;
                                    std::cout << "\nisTrue  1 ";
                                } else {         //add this else clause
                                    return false;
                                }
                            }
                        }
                    }
                }
            }
        } //for
    }
    std::cout << "\nisTrueCounter: " << isTrueCounter;
}

But some of your code can improve.

1.I don't know why you add these lines. This will never true unless word.length() is 0.

if (isTrueCounter == word.length())
{
    return true;
} 

2.Don't split condition that can write in one if into many if, it's a bad behavior.

if (word[i] == 'a' || word[i] == 'A')
{
    isTrueCounter  ;
    std::cout << "\nisTrue  1 ";
}
else
{
    if (word[i] == 'b' || word[i] == 'B')
    {
        isTrueCounter  ;
        std::cout << "\nisTrue  1 ";
    }

This is better.

if (word[i] == 'a' || word[i] == 'A' || word[i] == 'b' || word[i] == 'B')
{
    isTrueCounter  ;
    std::cout << "\nisTrue  1 ";
}

3.You can compare char, instead of check if word[i] == 'a'... Use > < >= <=.

if (word[i] >= 'a' && word[i] <= 'f' || word[i] >= 'A' && word[i] <= 'F')
{
    isTrueCounter  ;
    std::cout << "\nisTrue  1 ";
}

4.string is already in iostream you don't have to import it again.

In conclusion your code can turn into this

bool is_favorite(std::string word) 
{
    int isTrueCounter = 0;
    for (int i = 0; i < word.length(); i  )
    {
        if (word[i] >= 'a' && word[i] <= 'f' || word[i] >= 'A' && word[i] <= 'F') {
            isTrueCounter  ;
            std::cout << "\nisTrue  1 ";
        } else {
            return false;
        }
    }
    std::cout << "\nisTrueCounter: " << isTrueCounter;
    return true;
}
  • Related