[英]C++ Err: 'Called object type 'int' is not a function or function pointer'
I am attempting to write a quadratic equation calculator in C++, and I keep running into this error.我正在尝试用 C++ 编写一个二次方程计算器,但我一直遇到这个错误。 See if you can figure out why: (the error occurs on the 'g =' line)
看看你是否能找出原因:(错误发生在 '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;
}
}
Any help is appreciated!任何帮助表示赞赏!
The square root function will return floating point values, so you should use a floating point variable like double to store the result.平方根函数将返回浮点值,因此您应该使用像 double 这样的浮点变量来存储结果。
#include <cmath>
#include <iostream>
using namespace std;
int main(){
int a,b,c,e;
double d,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;
}
}
The problem is that the square root function returns a floating point decimal, and you are trying to combine it with an integer.问题是平方根函数返回一个浮点小数,而您正试图将它与一个整数组合。 For example, 1 - 2.0.
例如,1 - 2.0。 Both of the values have to be floating point decimals, so you should change "int" to "double" on line 5.
这两个值都必须是浮点小数,因此您应该在第 5 行将“int”更改为“double”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.