• 12-07-2006
godfrey270487
Code:

#include <iostream>
using namespace std;
void quadraticequation(float a, float b, float c);

int main()
{
int a,b,c;

cout <<"a"<< endl;
cin>>a;
cout<<"b"<<endl;
cin>>b;
cout<<"c"<<endl;
cin>>c;

}

void quadraticequation(float a, float b, float c)
{
if (((b*b)-4*a*c)<0)
{
cout<<"No real solution"<<endl;
}

else if (((b*b)-4*a*c)=0 )
{
}

else if (((b*b)-4*a*c)>0)
{
cout<<"X1 ="<< -b+sqrt((b*b)-4*a*c)/2*a;

cout<<"X2 ="<< -b-sqrt((b*b)-4*a*c)/2*a;
}

}

In the above code I'm to write out a function that solves a quadratic equation. I have one error that comes up but I dont know how to stop it because it seems okay to me(the beginner :rolleyes: ). Does anyone know where I went wrong??
• 12-07-2006
Desolation
Try to always tell us what the errors are when you get them. It seems to me the error comes from the fact that you have mistaken '==' for '='. It's in your first 'else if' statement.
• 12-07-2006
robatino
Well, mathematically you should have
Code:

cout<<"X1 ="<< (-b+sqrt((b*b)-4*a*c))/(2*a);
cout<<"X2 ="<< (-b-sqrt((b*b)-4*a*c))/(2*a);

You could also save some typing by defining a variable for b*b-4*a*c.

Edit: And also
Code:

• 12-07-2006
joan
Code:

#include <math.h>
#include <iostream>
using namespace std;
void quadraticequation(float a, float b, float c);

int main()
{
int a,b,c;

cout <<"a"<< endl;
cin>>a;
cout<<"b"<<endl;
cin>>b;
cout<<"c"<<endl;
cin>>c;

}

void quadraticequation(float a, float b, float c)
{
if (((b*b)-4*a*c)<0)
{
cout<<"No real solution"<<endl;
}

else if (((b*b)-4*a*c)==0 )
{
}

else if (((b*b)-4*a*c)>0)
{
cout<<"X1 ="<< (-b+sqrt((b*b)-4*a*c))/2*a;

cout<<"X2 ="<< (-b-sqrt((b*b)-4*a*c))/2*a;
}

}

• 12-07-2006
vart
there are two more places with /2*a that should be fixed
• 12-07-2006
godfrey270487
Thanks guys, It works now. I really appreciate the help, I have an exam in this Wednesday and up till today I was clueless, still am but I think I know the basics.