0

How do I write this C++ sentinel-controlled while loop?
I need to write a sentinel-controlled while loop to see if the coefficients of a quadratic equation have real roots using a nested decision. A character q must be my sentinel. This is what I have so far, but it doesn't really work and I'm getting lost...

# include <iostream>
# include <iomanip>
# include <cmath>
using namespace std;

int main ()
{
//Declare Variables
double a, b, c, r1, r2, descriminant;
char q;
char SENTINEL = q;

cout << setiosflags (ios::fixed)
<< setiosflags (ios::showpoint)
<< setprecision (3);

cout << "Input the Value for a or q to Quit: " << endl;
cin >> a; // read a

while (a!= SENTINEL)
{
if (a == 0)
cout << "Zero Divide" << endl;
else if (a != 0)
cout << "Input the Value for b or q to Quit: " << endl;
cin >> b; // read b
cout << "Input the Value for c or q to Quit: " << endl;
cin >> c; // read c
if (descriminant < 0)
cout << "No Real Roots" << endl;
else if (descriminant >= 0)
cout << setw(6) << "The Real Roots Are: " << endl;
cin >> r1; //read r1
cin >> r2; //read r2

descriminant = pow(b,2) - (4 * a * c);
r1 = (-b + (pow(b,2) - (4 * a * c))) / (2 * a);
r2 = (-b - (pow(b,2) - (4 * a * c))) / (2 * a);

cout <<"Root One is: " << r1 << endl;
cout <<"Root Two is: " << r2 << endl;
}


system ("PAUSE");

return 0;
}

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by Ancient Dragon
0

>> char q;
>> char SENTINEL = q;

The second line does nothing more than assign one char value to another. What's the value of 'q'? Answer: Undetermined because 'q' just contains whatever random value happens to be in the memory at the time.

If you want SENTINEL to represent when to exit the loop then assign it like this char SENTINEL = 'q';

Edited by Ancient Dragon: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.