Bool always evaluates as true

I am creating a program where you need to use the bool function to find out if three numbers are in ascending order when the user enters them. However, the bool function always evaluates to true. What am I missing? Here is my code:

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

      

+3


source to share


6 answers


You need to actually call the function:

if (inOrder(first, second, third))

      

it



if (inOrder)

      

always evaluates to true, since it actually checks if a function pointer is not specified.

+17


source


You will need to store the return value of the function and check for that - or just check the function directly. So:

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

if (result)
{
(...)

      

or



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

      

And the reason it if(inOrder)

always evaluates to true is because it checks the address of a function inOrder()

that is nonzero.

+9


source


Perhaps you meant

if (inOrder(first, second, third))

      

instead

inOrder(first, second, third);

if (inOrder)

      

when you say if (inOrder)

you don't actually call the function and check the result, instead you use the inOrder variable as a condition, which is nothing more than a pointer to the entry point of the function, which always evaluates to true.

+2


source


Try the following:

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

      

you are not taking a result from a function inOrder

+2


source


It's always true

because you are passing the address of the function to your if condition. Since the function will never be at address 0, the condition is always true. You need to either store the return value of the function:

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

      

or call the function in if:

if (inOrder(first, second, third))

      

+1


source


Here's a working copy. You need to keep o / p from your function call.

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

      

0


source







All Articles