Adding a "enter q to quit" for a calculator

I can't for the life of me figure out how to get the "enter Q to quit" to work properly. I don't know if I am allowed to do it this way, but can someone guide me in the right direction.
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
#include <iostream>

using namespace std;

int main()
{
  char calc;
  int num1, num2, ans, userAns;

  do{
    cout << "Select Math Operator(+, -, /, *, %) or enter Q to quit: ";
    cin >> calc;
    cout << endl;
    cout << "Enter two numbers between 1 and 20: ";
    cin >> num1 >> num2;
    cout << endl;

    switch (calc) {
      case '+': ans = num1 + num2;
      break;
      case '-': ans = num1 - num2;
      break;
      case '/': ans = num1 / num2;
      break;
      case '*': ans = num1 * num2;
      break;
      case '%': ans = num1 % num2;
      break;
    }
    cout << "Enter your answer: ";
    cin >> userAns;
    cout << endl;

    if (userAns == ans){
      cout << "Congratulations! That is the correct answer!" << endl;
    }
    else
      cout << "Wrong! The correct answer is " << ans << endl;
  }while(calc != 'q');
}
1
2
3
4
5
6
7
8
9
  while ( true )
  {
    cout << "Select Math Operator(+, -, /, *, %) or enter Q to quit: ";
    cin >> calc;
    if ( calc == 'q' || calc == 'Q' ) break;

    // The rest of your loop ...
   
   }

Topic archived. No new replies allowed.