How do I make loops for seperate cases


Hi, Im trying to make a switchcase calculator but I don't know how to make my other cases loop indefinitely while setting one of my cases as a breakpoint when prompted. Any suggestions?


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

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
	char operators;
	float number1;
	float number2;
	

	cout << "Enter your operators ( +, -, *, /). If you wish to terminate the program, press x" << endl;
	cin >> operators;

	switch (operators)
	{
	case '+':
		cout << "enter your numbers" << endl;
		cin >> number1;
		cin >> number2;
		cout << "This is the answer" << "\n" << number1 + number2 << endl;
		return 0;

	case '-':
		cout << "enter your numbers" << endl;
		cin >> number1;
		cin >> number2;
		cout << "This is the answer" << "\n" << number1 - number2 << endl;
		return 0;

	case '*':
		cout << "enter your numbers" << endl;
		cin >> number1;
		cin >> number2;
		cout << "This is the answer" << "\n" << number1 * number2 << endl;
		return 0;

	case '/':
		cout << "enter your numbers" << endl;
		cin >> number1;
		cin >> number2;
		cout << "This is the answer" << "\n"<< number1 / number2 << endl;
		return 0;

	case 'x':
		cout << "Goodbye, thank you for using switchase calculators." << endl;
		break;

	default: // if anything other than the aforementioned operators were entered.
		cout << "This is not a valid operator" << endl;
		return 0;
		
	}
	return 0;
}
Last edited on
Do you want the entire program to keep looping until the user wants to quit or do you want to keep looping inside the case with the same operation but different numbers?
Hello Jacketto,

All the case statements that end in return 0; cause the program to end before you have a chance to read the answer. All thereturn 0; s need to be changed tobreak;s. In order to keep the program running until 'x' is typed you will need somehing like this:

1
2
3
4
5
6
bool cont{ true };

do
{

} while (cont);


Put lines 13 to 54inside the "{}" of the do while loop. This will allow to program to loop until you type 'x'. You will need to add the line cont = false; to case 'x' to exit the do while loop. This works for the whole program, but the same concept will work inside the case statements.

Whether you havereturn 0; or break; you will exit the program or exit the switch
respectively. What I use to pause the program is:
std::this_thread::sleep_for(std::chrono::seconds(5)); this will require the header files "chrono" and "thread".

I have used case '-' as an example:

1
2
3
4
5
6
7
8
9
case '-':
	std::cout << "\n enter your numbers" << std::endl;
	std::cout << "\n number 1: ";
	std::cin >> number1;
	std::cout << "\n number 2: ";
	std::cin >> number2;
	std::cout << "\n This is the answer: " << number1 << " - " << number2 << " = " << number1 - number2 << std::endl;
	std::this_thread::sleep_for(std::chrono::seconds(5));
	break;


Hope that helps,

Andy
Last edited on
@handy Andy Thanks for the tip, it worked out as expected :D
Topic archived. No new replies allowed.