Reputation: 89
I'm making a program where you need to use a bool function to find out if three numbers are in ascending order when the user inputs them. However, the bool function always evaluates as 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;
}
Upvotes: 3
Views: 4975
Reputation:
Try this:
bool b = inOrder(first, second, third);
if(b){.....}
you are not taking result from inOrder
function
Upvotes: 2
Reputation: 573
Here is working copy. You need to store the 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;
}
Upvotes: 0
Reputation: 6092
You'll have to store the return value of the function, and test that - or just test the function directly. So:
bool result = inOrder(first, second, third);
if (result)
{
(...)
or:
if (inOrder(first, second, third)
{
(...)
And the reason for if(inOrder)
always evaluating to true is that it checks the address of the inOrder()
function, which is non-zero.
Upvotes: 9
Reputation: 17428
It's always true
because you are passing the address of the function into 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 the if:
if (inOrder(first, second, third))
Upvotes: 1
Reputation: 63707
May be you meant
if (inOrder(first, second, third))
instead of
inOrder(first, second, third);
if (inOrder)
when you say if (inOrder)
you are actually not calling the function and checking the result, instead you are using the variable inOrder as a condition which is nothing more than the pointer to the entry point of the function which always evaluates to true.
Upvotes: 2
Reputation: 23324
You need to actually call the function:
if (inOrder(first, second, third))
This
if (inOrder)
always evaluates to true, as it really checks, whether the function pointer is non-null.
Upvotes: 18