我正在製作一個程序,您需要使用布爾函數來查明三個數字是否在用戶輸入時按升序排列。但是,布爾函數總是評估爲真。我錯過了什麼?這裏是我的代碼:布爾總是評估爲真
#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