我正在制作一个程序,您需要使用布尔函数来查明三个数字是否在用户输入时按升序排列。但是,布尔函数总是评估为真。我错过了什么?这里是我的代码:布尔总是评估为真
#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之外的函数并且你不把结果赋值给一个值? – kappa 2012-04-04 15:28:28
'if(something){return true;} else {return false;}'可以(并且应该)总是重写为'return something;'。 – ipc 2012-04-04 15:29:00
+1生产一个完整的测试用例。 http://sscce.org/。 – 2012-04-04 15:54:26