C++ 布尔的评价总是正确的

C++ 布尔的评价总是正确的,c++,boolean,C++,Boolean,我正在制作一个程序,你需要使用bool函数来确定当用户输入三个数字时,它们是否按升序排列。但是,bool函数的计算结果始终为true。我错过了什么?这是我的密码: #include <iostream> #include <string> using namespace std; bool inOrder(int first, int second, int third) { if ((first <= second) && (secon

我正在制作一个程序,你需要使用bool函数来确定当用户输入三个数字时,它们是否按升序排列。但是,bool函数的计算结果始终为true。我错过了什么?这是我的密码:

#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;
}
#包括
#包括
使用名称空间std;
布尔顺序(整数第一、整数第二、整数第三)
{

如果((首先您必须存储函数的返回值,并进行测试,或者直接测试函数。因此:

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

if (result)
{
(...)
或:


if(inoorder)
总是计算为true的原因是它检查
inoorder()
函数的地址,该地址为非零。

您需要实际调用该函数:

if (inOrder(first, second, third))
bool ordered = inOrder(first, second, third);
这个

总是计算为true,因为它真正检查函数指针是否为非null。

可能是您的意思

if (inOrder(first, second, third))
而不是

inOrder(first, second, third);

if (inOrder)

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

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

if (inOrder(first, second, third))
bool ordered = inOrder(first, second, third);
或在以下情况下调用中的函数:

if (inOrder(first, second, third))
试试这个:

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

您没有按顺序从
函数中获取结果,这里是工作副本。您需要存储函数调用中的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;
}
#包括
#包括
使用名称空间std;
布尔顺序(整数第一、整数第二、整数第三)
{

if((首先,为什么在if之外调用函数而不将结果赋给值?
if(something){return true;}或者{return false;}
可以(而且应该)被重写为
return something;
+1以生成完整的测试用例。。太好了!非常感谢您。