【问题标题】:Bool always evaluating as true布尔总是评估为真
【发布时间】:2012-04-04 15:26:05
【问题描述】:

我正在编写一个程序,您需要在其中使用 bool 函数来确定三个数字在用户输入时是否按升序排列。但是,bool 函数的计算结果始终为真。我错过了什么?这是我的代码:

#include <iostream>
#include <string>

using namespace std;

bool inOrder(int first, int second, int third)
{
    if ((first <= second) && (second <= third))
    {
        return true;
    }

    else
    {
        return false;
    }
}

int main()
{
    int first, second, third;

    cout << "You will be prompted to enter three numbers." << endl;
    cout << "Please enter your first number: ";
    cin >> first;
    cout << "Please enter your second number: ";
    cin >> second;
    cout << "Please enter your third and final number: ";
    cin >> third;
    cout << endl;

    inOrder(first, second, third);

    if (inOrder)
    {
        cout << "Your numbers were in ascending order!" << endl;
    }

    else
    {
        cout << "Your numbers were not in ascdending order." << endl;
    }

    return 0;
}

【问题讨论】:

  • 为什么你在 if 之外调用函数并且你不将结果赋值给一个值?
  • if(something){return true;}else{return false;} 可以(并且应该)始终重写为return something;
  • +1 用于生成完整的测试用例。 sscce.org.

标签: c++ boolean


【解决方案1】:

你需要实际调用函数:

if (inOrder(first, second, third))

这个

if (inOrder)

总是评估为真,因为它真正检查函数指针是否为非空。

【讨论】:

  • 太棒了!非常感谢。
【解决方案2】:

您必须存储函数的返回值,并对其进行测试——或者直接测试函数。所以:

bool result = inOrder(first, second, third);

if (result)
{
(...)

或:

if (inOrder(first, second, third)
{
(...)

if(inOrder) 总是评估为 true 的原因是它检查了 inOrder() 函数的地址,该地址不为零。

【讨论】:

    【解决方案3】:

    可能是你的意思

    if (inOrder(first, second, third))
    

    而不是

    inOrder(first, second, third);
    
    if (inOrder)
    

    当你说if (inOrder) 时,你实际上并没有调用函数并检查结果,而是使用变量 inOrder 作为条件,它只不过是指向函数入口点的指针,它总是计算为 true .

    【讨论】:

      【解决方案4】:

      试试这个:

      bool b = inOrder(first, second, third);
      if(b){.....}
      

      您没有从 inOrder 函数中获取结果

      【讨论】:

        【解决方案5】:

        它始终是true,因为您将函数的地址传递给您的 if 条件。由于函数永远不会位于地址 0,因此条件始终为真。您需要存储函数的返回值:

        bool ordered = inOrder(first, second, third);
        

        或者调用if中的函数:

        if (inOrder(first, second, third))
        

        【讨论】:

          【解决方案6】:

          这是工作副本。您需要存储函数调用中的 o/p。

          #include <iostream>
          #include <string>
          
          using namespace std;
          
          bool inOrder(int first, int second, int third)
          {
              if ((first <= second) && (second <= third))
              {
                  return true;
              }
          
              else
              {
                  return false;
              }
          }
          
          int main()
          {
              int first, second, third;
          
              cout << "You will be prompted to enter three numbers." << endl;
              cout << "Please enter your first number: ";
              cin >> first;
              cout << "Please enter your second number: ";
              cin >> second;
              cout << "Please enter your third and final number: ";
              cin >> third;
              cout << endl;
              bool isordered;
              isordered = inOrder(first, second, third);
          
              if (isordered)
              {
                  cout << "Your numbers were in ascending order!" << endl;
              }
          
              else
              {
                  cout << "Your numbers were not in ascdending order." << endl;
              }
          
              return 0;
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-10-03
            • 1970-01-01
            • 1970-01-01
            • 2014-05-05
            • 2017-10-10
            • 2018-02-24
            相关资源
            最近更新 更多