在我的代碼中,我有幾個地方相同的cout
函數,我決定移出int main()
。 然而,當我這樣做,並重新運行我的代碼,當有問題的功能被觸發時,我收到「調試斷言失敗」錯誤。調試斷言在cout功能失敗
我已經放在主函數之外的代碼。
string printresult(double smallest, double largest)
{
cout << "the smaller value is: " << smallest << "\n";
cout << "the larger value is: " << largest << "\n";
return 0;
}
我相信它有什麼做的return 0
代碼,我在cout
函數的最後增加了,沒有它然而,我的代碼不會現在連編譯。任何人都可以指向正確的方向,這樣我可以更好地識別我的錯誤?
我完整的代碼
#include "../../std_lib_facilities.h"
// conversion of units to meters
double funconvert(double x, string unit)
{
const double cm_m = 1.0/100.0, in_cm = 2.54, ft_in = 12;
if (unit == "m")
return x = x;
else if (unit == "cm")
return x = x * cm_m;
else if (unit == "in")
return x = x * in_cm * cm_m;
else if (unit == "ft")
return x = x * ft_in * in_cm * cm_m;
else
cout << "Unknown unit value.\n";
}
// printing the large and the small variables
string printresult(double smallest, double largest)
{
cout << "the smaller value is: " << smallest << "\n";
cout << "the larger value is: " << largest << "\n";
return 0;
}
int main()
{
vector<double>numbers;
double a, b, smallest, largest;
string unit;
cout << "Please enter an intiger followed by a measurment unit [e.g 10cm]:";
// Take input
while (cin >> a >> unit)
{
// check if this is the first number entered
if (numbers.size() == 0)
{
a = funconvert(a, unit);
b = a;
numbers.push_back(a);
numbers.push_back(b);
}
else
{
//assign a and b to the last two numbers in a vector
a = funconvert(a, unit);
numbers.push_back(a);
a = numbers[(numbers.size() - 1)];
b = numbers[(numbers.size() - 2)];
// numbers.erase(numbers.begin()); // enable if desire to keep the vector empty
}
// find out which variable is larger and smaller
if (a > b && (a/b-1) >= 0.01)
{
smallest = b;
largest = a;
printresult(smallest, largest);
}
else if (a < b && (b/a - 1) >= 0.01)
{
smallest = a;
largest = b;
printresult(smallest, largest);
}
else if ((a/b - 1) < 0.01 && (a/b - 1) != 0.00 || (b/a - 1) < 0.01 && (a/b - 1) != 0.00)
{
cout << "the nubers are almost equal\n";
}
else
cout << a << " equals " << b << "\n";
}
return 0;
}
您聲明'printresult'會返回一個'string'更換特質
return x = x;
,但你的身體返回'int'。相反,它看起來應該是'void printresult(...)',因爲你沒有任何東西可以返回? – crashmstr爲什麼要從預期返回'string'的函數返回'0'? – AndyG
@AndyG即使現在回想起來也沒什麼意義,我這樣做是爲了讓我的程序能夠編譯,也許這樣我就能夠在邏輯上跟蹤我的錯誤。毋庸置疑,這並沒有奏效。感謝您的意見,它幫助了很多!現在我會知道。 – Newskooler