【发布时间】:2015-10-01 16:00:40
【问题描述】:
我正在尝试用 C++ 编写一个二次方程计算器,但我一直遇到这个错误。看看你是否能找出原因: (错误发生在 'g =' 行)
#include <cmath>
#include <iostream>
using namespace std;
int main(){
int a,b,c,d,e,f,g;
cout<<"Enter 'A' value:";
cin>>a;
cout<<"Enter'B' value:";
cin>>b;
cout<<"Enter 'C' value:";
cin>>c;
e = ((b * b) - (4 * (a*c)));
d = sqrt(e);
f = ((-b) + d) / (2 * a);
g = ((-b) - d) / (2 * a);
if(d > 0){
cout<<"One solution is: "<<f<<endl;
cout<<"The other solution is: "<< g <<endl;
}
else if(d == 0){
cout<<"Your 1 solution is: "<< f <<endl;
}
else{
cout<<"No real solutions!"<<endl;
}
}
感谢任何帮助!
【问题讨论】:
-
您应该查看整数除法和浮点除法之间的区别。你真的要使用整数除法吗?
-
顺便说一句,square root function 返回一个浮点值,而不是整数。
-
@ThomasMatthews 不,你没有。如果控制从
main流出而没有return语句,则暗示retun 0。 -
我在 Cygwin for Windows 7 上使用
g++版本 4.9.2 时没有出现编译错误。 -
Works for me。我猜在你的真实代码中,你忘记了
/字符。