immediate exit while loop

Oct 22, 2010 at 5:13pm
I have to write this program with a sentinel controlled loop, with 'q' as the sentinel. The issue I am having, is that when I enter 'q' I need it to exit the loop immediately and bypass the equations and such. As it stands, everything works, except that when I enter 'q', it still performs and displays the calculation. Any help/ hints would be much appreciated.

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

int main ()
{
float a;
float b;
float c;
float r1;
float r2;
const char SENTINEL = 'q';




cout << "Enter 'q' at any time to terminate\n";
cout << "Enter value 'A':\n";
cin >> a;
cout << "Enter value 'B':\n";
cin >> b;
cout << "Enter value 'C':\n";
cin >> c;

while (a != SENTINEL && b != SENTINEL && c != SENTINEL)

{
if (a > 0)
{
r1 =(-b+(sqrt(pow(b,2)) - (4*a*c))/(2*a));
r2 =(-b-(sqrt(pow(b,2)) - (4*a*c))/(2*a));
cout << "Root 1: " << r1 << "\n" << "Root 2: " << r2 << "\n";
break;
}
else if (a < 0)
cout << "Error: Negative Discriminant\n";
else if (a == 0)
cout << "Error: Zero Divide\n";
break;
}

system ("pause");
return 0;
}
Oct 22, 2010 at 5:14pm
I've tried moving my while statement to different parts of the program, and I've tried writing it different ways, this version is just my latest attempt
Oct 22, 2010 at 5:18pm
You are comparing a char value with a float. You change data type of your SENTINEL or change data type your input(a,b,c).
Oct 22, 2010 at 5:21pm
I had had that same thought, but I don't know how I can do that, since I HAVE to use 'q' as my sentinel value, and I HAVE to have number input values.
Oct 22, 2010 at 5:30pm
" 3) Write a sentinel controlled while loop based on a character value to control the loop. 'q' or 'Q' will terminate the loop, any other value will continue processing. Do not use a do-while loop. If the first input is 'q' the program should bypass the loop." I've been trying to do this for about a week now with no success.
Topic archived. No new replies allowed.