do-while loops to stop with a char

I would like to make my do-while loop stop using a char. is that possible or should i use just a while loop? What is the problem in my code, it runs but when i press Q it turns into a continuous loop?

p.s. there's a longer program to this code i just didnt add it

1
2
3
4
5
6
7
    do {

        cout << "Enter the Operator: ";
        cin >> operators;
        cout << "Would you like to stop? Enter Q to stop" << endl << endl;

       } while (operators != 'Q');
Last edited on
What if the user enters a lowercase q ('q')?
Did you declare "operators" as a character or a string?

You shouldn't get a continuous loop. You can certainly do this with a do-while loop.
i declared operators as a character, and there is a table for the user that has all the operators and their functions.

So i ran the program again and it doesn't give me a continuous loop, my mistake, but the program continues on when it should have stopped.

Here's the entire main body

Last edited on
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
int main()
{
    int operand1, operand2;
    double result = 0, number = 0;
    char operators;
    bool Q = false;
    
    menu();
    do {

        cout << "Enter the Operator: ";
        cout << endl << "Enter Q as the operator to stop" 
             << endl << endl;
        cin >> operators;
        
        if (operators != 'R' || operators != 'F' || operators != 'T') {
            cout << "First Number: ";
            cin >> operand1;
            cout << "Second Number: ";
            cin >> operand2;
            result = calculate(operand1, operand2, operators);
            cout << operand1 << " " << operators << " " << operand2
                 << " = " << result << endl << endl;
            }                        
        else if (operators == 'R') {
            cout << "What number would you like to find the reciprocal of: ";
            cin >> number;        
            result = reciprocal(number);
            cout << "Reciprocal of " << number << " is " << result << endl;
            }
        else if (operators == 'F') {
            cout << "What number would you like to find the factorial of: ";
            cin >> number;
            result = factorial(number); 
            cout << "Factorial of " << number << " is " << result << endl;                       
            }
        else if (operators == 'T') {
            cout << "What number would you like to square root: ";
            cin >> number;
            result = sqrt(number); 
            cout << "Square Root of " << number << " is " << result << endl;
            }
        else if (operators != 'Q') //now if will not output this if they enter 'Q'
             cout << "Error. You have not typed in a correct operator." << endl;           
                    
        
    } while (operators != 'Q');                                      //This does not work
    
    system("PAUSE");
    return 0;
}

I can't test this because I don't have your function definitions, but it worked when I commented out the stuff in-between line 13 and 47 (and also line 8)
It will only check whether operators is equal to Q or not at the end of the program, that's why when you enter 'Q' operator at the beginning, you will have to go through the entire program before it stops.
Last edited on
This could be a solution.

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
58
59
#include <iostream>
using namespace std;


int main()
{
    int operand1, operand2;
    double result = 0, number = 0;
    char operators;
    bool Q = false;
    
    menu();
    do {

        cout << "Enter the Operator: ";
        cin >> operators;
        if(operators == 'Q' || operators == 'q'){
            goto END;
        }
        if (operators != 'R' || operators != 'F' || operators != 'T') {
            cout << "First Number: ";
            cin >> operand1;
            cout << "Second Number: ";
            cin >> operand2;
            result = calculate(operand1, operand2, operators);
            cout << operand1 << " " << operators << " " << operand2
                 << " = " << result << endl << endl;
            }                        
        else if (operators == 'R') {
            cout << "What number would you like to find the reciprocal of: ";
            cin >> number;        
            result = reciprocal(number);
            cout << "Reciprocal of " << number << " is " << result << endl;
            }
        else if (operators == 'F') {
            cout << "What number would you like to find the factorial of: ";
            cin >> number;
            result = factorial(number); 
            cout << "Factorial of " << number << " is " << result << endl;                       
            }
        else if (operators == 'T') {
            cout << "What number would you like to square root: ";
            cin >> number;
            result = sqrt(number); 
            cout << "Square Root of " << number << " is " << result << endl;
            }
        else 
             cout << "Error. You have not typed in a correct operator." << endl;           
                    

        cout << endl << "Enter Q as the operator to stop" 
             << endl << endl;
      END:;
    } while (operators != 'Q');                                      //This does not work
    
    system("PAUSE");
    return 0;
}
Topic archived. No new replies allowed.