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;
}
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
" 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.