Real Roots of the Quadratic Formula

I can't get an answer for the quadratic; it just skips right to the "Try again?"

int main()
{
double a, b, c;
double Pos;
double Neg;
char tryagain;

cout << "Enter the coefficient of the quadratic term" << endl;
cin >> a;
cout << "Enter the coefficient of the linear term" << endl;
cin >> b;
cout << "Enter the constant term" << endl;
cin >> c;

do{

if(b*b > 4.0*a*c){
Pos =(-b+sqrt((b*b)-(4*a*c)))/(2*a);
cout << "The positive root is " << setprecision(5) << Pos << endl;

Neg =(-b-sqrt((b*b)-(4*a*c)))/(2*a);
cout << "\nThe negative root is " << setprecision(5) << Neg << endl;

}



else if(b*b < 4*a*c){
cout << "Complex Roots!";
}

cout << "\n Try again?"; //Asks user if they want to pick a new N for a sum
cin>> tryagain;

cin.ignore(80,'\n');

}while( tryagain == 'y' || tryagain == 'Y');




system("PAUSE");
return(0);
What if b^2==4*a*c?

By the way, your wording isn't quite right. Not in every pair of roots one has the opposite sign of the other.
Ya I realized that, I just figured I'd get it to work before I worried about the wording. Thanks.
Last edited on
Don't use cin >>foo. It leaves characters in the input buffer and it all gets messy later on. Use std::getline() instead. There are articles some member of the forum have written about various ways of getting input.
You need to include the prompting for the variables in your loop.
Did you count the number of }'s and {'s check those. Seems like you have uneven numbers that is just letting you exist from the do while loop.
sdhak002: It only looks that way at first because OP didn't use any code tags. The braces are matched.
I recommend adding a few output statements right after input to ensure the data is correct. For example, right after inputting a, b, and c. That way, you can confirm that the inputs are correct before proceeding further.

Also, I am curious: what data do you enter?
Topic archived. No new replies allowed.