Help with sentinel loop

I have to write a program to solve a quadratic equation and have different outputs based on the input (zero divide, no real roots, and the answer). I'm having problems right now making the program not find roots if there is an error condition, and I'm also get a repeating output when I use 'Q' to terminate the loop. Here is what I currently have I hope someone can help point out whats wrong with what I got.



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

using namespace std;

char Q='\0';
const char SENTINEL='Q';

int main ()
{
float A, B, C;
float root1, root2;
float discr;
while (Q!=SENTINEL)
{

cout<<"Enter values of A, B, and C or Q to terminate the loop\n";
cin>>A>>B>>C;

if (A==0)
cout <<"Zero divide"<<endl;
else if (discr<0)
cout <<"No real roots"<<endl;
else if (discr>0)
discr=pow(B,2)-(4*A*C);
root1=(-B - sqrt(B * B - 4 * A * C)) / (2 * A);
root2=(-B + sqrt(B * B - 4 * A * C)) / (2 * A);
cout<<"The two roots are"<<root1<<"and"<<root2<<endl;
}

system ("pause");
return 0;
}
I'm also get a repeating output when I use 'Q' to terminate the loop.

You may want to look into breaking out of the loop when you enter 'Q' into on of your variables.

I'm having problems right now making the program not find roots if there is an error condition


First I recommend you use braces {} with your condition statements, even if not actually required. Then find an indentation style you like and use it consistently. http://en.wikipedia.org/wiki/Indent_style Next since you are using an if/else chain if you place your calculations in the "non" error clause you will only do the calculations when you don't have an error.

I also think you need to restudy your "error" conditions. What happens in your program if discr is equal to zero?

Topic archived. No new replies allowed.