Hello all,

As my name indicates, I am a new programmer. I was doing a program which calculates the roots of a quadratic equation but unfortunately there seems to be something wrong as it ignores my if statements!

Please take a look at my code and I'd appreciate your help

Attachments
``````# include <iostream>
# include <cmath>

using namespace std;
int main()
{
double a, b, c, x, delta, f;
a=0;
b=0;
c=0;

cout << "Welcome to The Real Roots Calculator" << endl;
cout << "Please Enter a, b, c seperated by a space. Then press return" << endl;

cin >> a;
cin >> b;
cin >> c;

delta = (b*b)-(4*a*c);

f = sqrt(delta);

if (delta > 0)
{  cout << "The roots are:" << endl;
cout << "x1 =" << ((-1*b) + f)/(2*a) << endl;
cout << "x2 =" << ((-1*b) - f)/(2*a) << endl; }

else if (delta = 0)
{ cout << "There is one Unique Zero" << "x =" << -b /(2*a) << endl; }

else if (delta < 0)
{  cout << "Sorry! There are no real roots for this equation" << endl; }

cout << "Thank you for using this program" << endl;

return 0;
}``````
3
Contributors
3
Replies
4
Views
8 Years
Discussion Span
Last Post by new programer
``else if ( delta == 0 ) // compare, don't assign!``
``else if ( delta == 0 ) // compare, don't assign!``

Yeah, thats a common mistake (i think we all made that one when we were starting out) :)

oh-uh that totally slipped off my mind .. Thanks a lot .. it works now! :)