【发布时间】:2017-10-14 17:34:33
【问题描述】:
我正在尝试编写一个程序来使用函数计算二次方程的输出,但 Dev C++ 不断给我输出“[Error] Id 返回 1 退出状态”。 我是 C++ 的新手,如果我犯了一些愚蠢的错误,请提前道歉。
#include <iostream>
#include <cmath>
using namespace std;
float equation1 (float, float, float);
float equation2 (float, float, float);
main()
{
float a, b, c, Res1, Res2;
cout << "Insert the parameters of the equation.\n";
cin >> a >> b >> c;
if (a == 0)
{
Res1 = b / c;
cout << "It's a 1st degree eq. and the result is " << Res1 << endl;
}
else
{
Res1 = equation1 (a, b, c);
Res2 = equation2 (a, b, c);
cout << "The results of the eq. are " << Res1 << " and " << Res2 << endl;
}
system ("pause");
return 0;
}
float equation1 (double a, double b, double c)
{
float D, Res1;
D = (b * b) - 4 * a * c;
Res1 = (- b + sqrt(D)) / (2 * a);
return Res1;
}
float equation2 (double a, double b, double c)
{
float D, Res2;
D = (b * b) - 4 * a * c;
Res2 = (- b - sqrt(D)) / (2 * a);
return Res2;
}
【问题讨论】:
-
欢迎来到 Stack Overflow。请花时间阅读The Tour 并参考Help Center 中的材料,您可以在这里问什么以及如何问。
-
我想你可能打算写
int main(){ ...}。其次,链接器找不到equation1(float, float, float)的定义,同样equation2(float, float, float)。请注意,定义的函数float equation1(double, double, double)重载了您的第一个声明。 -
@WhiZTiM 我应该怎么做才能避免过载?
-
... 编译后你可能会注意到你没有检查
c != 0(你会在Res1 = b / c;中得到一个除零)和D >= 0在尝试获取它的@ 987654331@. -
@Sam - 你在上面的声明中有
float参数和函数体所在的double参数。
标签: c++ function equation dev-c++