I need help with this program and I'm not sure how to do something. The program is supposed to take 3 doubles to make a quadratic equation, then calculate and display all real roots. Here's the problem: If one or more of the roots is unreal or complex, it tries to display them anyway because I don't know how to test for that.
Here's the relevant parts:
Obviously, those if statements aren't of much use. So, is there some method or something to test the variables to see whether they're defined?
Here's the relevant parts:
Code:
double a, b, c, root1, root2;
cout << "Enter the 3 coefficients of the quadratic equation." << endl;
cin >> a >> b >> c;
if(cin)
{
cout << "Last input was successful." << endl;
cout << a << "x" << (char)253 << " + " << b << "x + " << c << endl;
root1 = (-b + sqrt(pow(b,2)+4*a*c)) / (2*a);
root2 = (-b - sqrt(pow(b,2)+4*a*c)) / (2*a);
if(root1 && root2)
cout << "There are two real roots." << endl;
else if(root1 || root2)
cout << "There is one real root." << endl;
else cout << "All roots are complex numbers." << endl;
if(root1)
cout << "Root 1 = " << root1 << endl;
if(root2)
cout << "Root 2 = " << root2 << endl;
}
else
cout << "Last input was unsuccessful. Check your data and try again." << endl;
Comment