返回值时布尔检查表现异常

发布于 2025-01-14 14:13:20 字数 2748 浏览 1 评论 0原文

#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!";
    }
}

这是我的代码,我试图将一个字符串传递到一个布尔函数中,如果传递的字符串满足所有条件,该函数将返回 true。 “通过”单词的资格是它只包含字母 af (无论哪种情况),因此像 AaAa 或 Cafe 或 Bad 这样的单词应该通过,但经过反复试验,即使是我知道应该通过的单词也失败了,我觉得就像我通过递增变量 (isTrueCounter) 来正确跟踪字母的限定条件一样,以计算字符串中的所有字符是否都是限定字符,但即使函数应该返回 true,也会显示 false 情况。我做错了什么?我没看到什么?当我运行此代码时,它将显示变量以帮助跟踪何时将内容添加到持有者变量中,但即使所有数字都正确,也会显示错误的情况。

#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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

空宴 2025-01-21 14:13:20

您没有返回任何东西,您可以通过多种方式做到这一点。例如,在您的代码中,在条件不成立后立即返回 false(不在 af 中)。

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;
}

但您的某些代码可以改进。

1.我不知道你为什么要添加这些行。除非 word.length() 为 0,否则这永远不会成立。

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

2.不要将可以在一个 if 中写入的条件拆分为多个 if ,这是一种不好的行为。

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 ";
    }

这更好。

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

3.您可以比较 char,而不是检查 word[i] == 'a'... 使用 >; < >= <=

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

4.string 已经在 iostream 中,您不必再次导入它。

总之你的代码可以变成这样

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;
}

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文