#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

int main ()
{
float a, b, c, disc, t;
int dec;

cout << "We are going to calculate the roots of ax^2 + bx +c using the quadratic equation";
cout << "to begin, enter values for a, b, c, and x.";
cout << "\na = " <<flush;
cin >> a;
cout <<"b = " <<flush;
cin >> b;
cout << "c = " <<flush;
cin >> c;

cin.get();
cout << "We will treat the discriminant, b^2-4ac, to be zero if |b^2-4ac| < t, where t";
cout << "\nis the tolerance value, enter a value for t. Then enter the number of decimal";
cout << "\nplaces you want for the answers.";
cout <<"\nt = ";
cin >> t;
cout << "Number of decimal places = ";
cin >> dec;
cin.get();
disc = (b*b - 4*a*c);

if (disc >= t)
{
cout <<"\nThe two real roots are: \nx1 = " << fixed << setprecision(dec) << ((b*-1) + sqrt(disc)/(2*a));
cout <<"\nx2 = "<< fixed << setprecision(dec) << ((b*-1) - sqrt(disc)/(2*a));
}
else if (disc <= -t)
{
cout <<"\nYou have two complex roots: \nx1 = "<< fixed << setprecision(dec) << ((b*-1)/(2*a)) + (sqrt(abs(disc))/(2*a)) <<"i";
cout <<"\nx2 = "<< fixed << setprecision(dec) <<((b*-1)/(2*a)) - sqrt((abs(disc)))/2*a <<"i" <<endl;
}
else
{
//abs(disc) < t);

cout <<"\nThe single root of the quadratic equation is " << fixed << setprecision(dec) << (b*-1/(2*a));
}

cout <<"\n\nPress enter to exit the program"<<endl;
cin.get();

return 0;
}
*/ I am not sure why this is not working, help would be very much appreciated */
Last edited on
Exactly what is going wrong?

Is it a compiling error? Execution error? Something else?

Not sure if this is the error you are talking about, but I notice you handle the tolerance wrong. tolerance will be a positive number; it is simply a magnitude for how much error you are willing to accept. I don't see why you are manipulating it by negating it. And the discriminant should simply be compared to 0, to see if it is positive, negative or zero. Not sure why you are comparing it to tol at all.

Also, you cout a statement that the user should enter values for a, b, c, and x. But there is no x variable ever declared or used in the program.
Also, your complex roots will not be displayed correctly. Your roots are displayed as purely complex numbers. For example, instead of displaying 1+3i and 1-3i you display 4i and -2i. You need to display first the real part, then the imaginary part, and then the letter "i"
Topic archived. No new replies allowed.