terminating a while loop with a char

I can't seem to terminate this program with char 'q'. I have no problems with the program being modified to terminate by an int.
As it is now, the program will turn into an infinite loop when "q" is used.
I'm sure it has something to do with my initialization, but I can't see it (or lack the knowledge)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <cmath>
#include <math.h>
#include <iomanip>

using namespace std;

char q ;
const char SENTINEL = 'q' ;

int main ()

{
    
    double x1, x2, a, b, c ;
    
    cout << "This program will solve for the Quadratic Formula\n";
    cout << "Input values as a, b, and c; or q to quit \n" ;
    cout << "a = " ;
    cin >> a ;
    
    while( a != SENTINEL )   
    {
    cout << "b = " ;
    cin >> b ;
    cout << "c = " ;
    cin >> c ;
        if (a > 0 && ((b * b - 4 * a * c) > 0))
		{
         x1 = (-b - sqrt(b * b - 4 * a * c)) / (2 * a) ;
         x2 = (-b + sqrt(b * b - 4 * a * c)) / (2 * a) ;
	 cout << " The roots are " << x1 << "and " << x2<< endl;
		 
		}
        else if ( a == 0)
		{
         cout << "Zero divide" << endl;
        
        }
        else 
	{
	 cout << "No real roots" << endl;
		
     	}
      cout << "Input values of a, b, and c; or q to quit \n" ;
      cout << "a = " ;
      cin >> a ;
    } 
    system ("PAUSE");
    
	return (0);
}
Last edited on
'a' is a double, not a char

article on input validation:
http://www.cplusplus.com/forum/articles/6046/
Your problem is that the variable a is a double.
I suggest setting char q = '\0' or to something other than 'q'. Then move the start of your while loop to before the cin >> a.

It will look something like this:
1
2
3
4
5
6
7
8
while( q != SENTINEL )
{
  cin >> a;
  /* Quadratic Stuff */

 cout << "Enter \'q\' to quit or any other character to continue: ";
 cin >> q;
}


A do-while loop would be better in this situation but you can do what you want.


Edit: Too slow...
Last edited on
Seriously I had a dumb moment, not being able to compare char to a double makes perfect sense.

However, I taking a a first year class, and I'm limited in my libraries. I have read a lot about using string to handle this problem, but I won't work for me.

"A do-while loop would be better in this situation but you can do what you want. "

I agree, but I have to have a terminate command from the start
Last edited on
Have you read my post. All you have to do is copy the quadratic stuff to inside the while loop before the "Enter q to ...".

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <cmath>
#include <math.h>
#include <iomanip>

using namespace std;

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

int main ()

{
    
    double x1, x2, a, b, c ;
    
    cout << "This program will solve for the Quadratic Formula\n";
  
    cout << "Would you like to continue? (q to quit, any other key continues)\n";
    cin >> q;
    while( q != SENTINEL )   
    {
    
    cout << "Input values as a, b, and c\n" ;
    cout << "a = " ;
    cin >> a ;

    cout << "b = " ;
    cin >> b ;
    cout << "c = " ;
    cin >> c ;
        if (a > 0 && ((b * b - 4 * a * c) > 0))
		{
         x1 = (-b - sqrt(b * b - 4 * a * c)) / (2 * a) ;
         x2 = (-b + sqrt(b * b - 4 * a * c)) / (2 * a) ;
	 cout << " The roots are " << x1 << "and " << x2<< endl;
		 
		}
        else if ( a == 0)
		{
         cout << "Zero divide" << endl;
        
        }
        else 
	{
	 cout << "No real roots" << endl;
		
     	}

      cout << "Enter \'q\' to quit or any other character to continue: ";
      cin >> q;

    } 
    system ("PAUSE");
    
	return (0);
}


edit: fixed, added terminate command to start
Last edited on
Thanks, it gave me a new way of looking at it. I want to play with it more to make better sense of it
I have read a lot about using string to handle this problem, but I won't work for me.

Using strings to validate input is a really good thing, try this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
string inpt;
while(true)
{
    cout << "Input values as a, b, and c; or q to quit \n" ;
    cout << "a = " ;
    cin >> inpt;
    if ( inpt[0]=='q' ) break;//exit from the loop

    //The loop is continuing so you don't need the else
    stringstream ss(inpt);
    ss >> a; // if the 1st character wasn't 'q' set the contents of the input to a

    //...
}

( You should do even more input validation than this )
Last edited on
Topic archived. No new replies allowed.