Switch Statements with Modifications Help

Good day to everyone. Can someone lend me a hand in modifying the code below? We are needed to modify the code below by inserting a "PRESS ANY KEY TO CONTINUE below" in it as instructed by our teacher as our assignment. And when I press any key, It will somehow restart to "Enter an operator (+, -, *, /):"

I'm working on it for hours and I somehow can't successfully do it. Also, I need to point out that the system should stop printing the main menu for a moment unless a key should be printed first.

// Program to build a simple calculator using switch Statement
#include <iostream>
#include <conio.h>
using namespace std;

int main() {
    char oper;
    float num1, num2;
    cout << "Enter an operator (+, -, *, /): ";
    cin >> oper;
    cout << "Enter two numbers: " << endl;
    cin >> num1 >> num2;

    switch (oper) {
        case '+':
            cout << num1 << " + " << num2 << " = " << num1 + num2;
            break;
        case '-':
            cout << num1 << " - " << num2 << " = " << num1 - num2;
            break;
        case '*':
            cout << num1 << " * " << num2 << " = " << num1 * num2;
            break;
        case '/':
            cout << num1 << " / " << num2 << " = " << num1 / num2;
            break;
        default:
            // operator is doesn't match any case constant (+, -, *, /)
            cout << "Error! The operator is not correct";
            break;
    }

    return 0;
}
Last edited on
As you're using conio.h, then perhaps something like:

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
#include <iostream>
#include <cstring>
#include <conio.h>

using namespace std;

int main() {
	const char* ops {"+-*/"};
	char oper {};

	do {
		cout << "\nEnter an operator (+, -, *, /, q(uit)): ";
		cin >> oper;

		if (oper != 'q' && oper != 'Q') {
			if (strchr(ops, oper) != NULL) {
				float num1 {}, num2 {};

				cout << "Enter two numbers: ";
				cin >> num1 >> num2;

				switch (oper) {
					case '+':
						cout << num1 << " + " << num2 << " = " << num1 + num2;
						break;

					case '-':
						cout << num1 << " - " << num2 << " = " << num1 - num2;
						break;

					case '*':
						cout << num1 << " * " << num2 << " = " << num1 * num2;
						break;

					case '/':
						cout << num1 << " / " << num2 << " = " << num1 / num2;
						break;
				}
			} else
				cout << "Error! The operator is not correct";

			cout << "\nPress any key to continue:";
			_getch();
		}
	} while (oper != 'q' && oper != 'Q');
}

Thanks for the help bro. I somehow can't successfully do it. Also, I need to point out that the system should stop printing the main menu for a moment unless a key should be printed first.
Last edited on
"PRESS ANY KEY TO CONTINUE below" in it as instructed by our teacher as our assignment. And when I press any key, It will somehow restart to "Enter an operator (+, -, *, /):"


If what I posted above isn't what is expected, then you need to more specific re the requirement.

It will display the 'menu', obtain the operation, then the 2 numbers as appropriate, then display the calculation, then display the Press Any Key message, and then re-display (re-start) the menu when any key is pressed.
I apologize for it, bro. It is my fault. I'll be more specific in questioning next time. Anyways. Thanks for the help. I somehow managed to figure out it on my own. That code gives me a reference and because of it I successfully did it.
Last edited on
Topic archived. No new replies allowed.