我正在制作一个程序,您需要使用 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 (inOrder(first, second, third))


if (inOrder)

始终评估为真,因为它确实会检查函数指针是否为非空。

关于c++ - Bool 总是评估为真,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10014264/

10-13 08:20