Don't know what's wrong with the code

Here's the problem. I've written a code but can't correct the errors in it.Here's the question and the code:

"Write a C++ function named quadratic that given three numbers a, b, c solves quadratic equations using the formula: x^2 = (-(b^2) + b-4ac) / (4a^2)
The function returns
• -1 if the equation is not quadratic (a = 0)
• 0 if the equation has no real solution (b2 – 4ac < 0)
• or the result
You can use the standard library function to compute the square root."


#include<iostream>
#include<cmath>
using namespace std;

float quadratic ( float a, float b, float c )
{
float x, y;
x = (-b + sqrt ((b*b) - (4*a*c)))/(2*a);
y = (-b - sqrt ((b*b) - (4*a*c)))/(2*a);
if((b*b)-(4*a*c)){
return 0;
}
else if(a == 0){
return -1;
}
else{
cout<<"Answer is either "<<x<<" or "<<y<<endl;
return x, y;
}
}
int main()
{
int d, e, f;
cout<<"Enter first number: ";
cin>>d;
cout<<"Enter Second number: ";
cin>>e;
cout<<"Enter third number: ";
cin>>f;
quadratic (d, e, f);
return 0;
}
Last edited on
#include<iostream>
#include<cmath>
using namespace std;

float quadratic ( float a, float b, float c )
{
float x, y;
x = (-b + sqrt ((b*b) - (4*a*c)))/(2*a);
y = (-b - sqrt ((b*b) - (4*a*c)))/(2*a);
if(((b*b)-(4*a*c)) < 0){
cout << 0 << "\n";
return 0;
}
else if(a == 0){
cout << -1 << "\n";
return -1;
}
else{
cout<<"Answer is "<<x<<" or "<<y<<endl;
return x, y;
}
}
float main()
{
float a, b, c;
cout<<"Enter first number: ";
cin>>a;
cout<<"Enter Second number: ";
cin>>b;
cout<<"Enter third number: ";
cin>>c;
quadratic (a, b, c);
return 0;}

Try that ;]

When you started manipulating the numbers, you were giving the computer undefined constants, so it didn't really know what to do. You may also have received a warned about converting the integers to floated values, which is solved by just floating the inputted values.
Last edited on
still I'm getting these errors:
3lab4.cpp: In function âfloat quadratic(float, float, float)â:
3lab4.cpp:23: warning: left-hand operand of comma has no effect
3lab4.cpp: At global scope:
3lab4.cpp:26: error: â::mainâ must return âintâ
Did you copy/paste my code, or just edit some of your own?

It seems as if you didn't edit a couple of the lines, because I've run this thing in three different compilers now and it runs perfectly every time.

Do you have "float main()"?


Last edited on
Let me try editing some few things but yes I did copy/pasted your work
K, also, just so you know, I edited my original code with two extra parentheses here:

if(((b*b)-(4*a*c)) < 0)

It doesn't want to return anything with this conditional without the extra ones....
I got these errors after changing "float main ()" to "int main":
3lab4.cpp: In function âfloat quadratic(float, float, float)â:
3lab4.cpp:20: warning: left-hand operand of comma has no effect
What does the first error mean or imply?
The extra parentheses don't have any effect. I still get the same errors
A function can't return more than one value. return x,y is wrong. What the warning (a warning is not an error) is saying is that x in x,y has no effect.
Finally it has run. Thanks a million. :-)
Wow, that makes me wonder why my compilers gave me 0 grief over that..
Topic archived. No new replies allowed.