Home / utk / cs102 / fa16 / labd / code_snippets / switch_dowhile_example.cpp
Directory Listing
arrays_lab_session1.cpp
arrays_lab_session2.cpp
example.cpp
labD_pseudo_code.txt
switch_dowhile_example.cpp
switch_example.cpp
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
#include <iostream>

using namespace std;

int main() {
	char option,
		 repeat;

	int x, y;
	
	do {
		cout << "Welcome! Here are some options..." << endl;
		cout << "  1 - Addition\n  2 - Subtraction\n  3 - Multiplication\n  4 - Division" << endl;
	
		cout << "Select an operation: ";
		cin >> option;
		
		cout << "Input X: ";
		cin >> x;
		cout << "Input Y: ";
		cin >> y;
	
		switch (option) {
			case '1':
				//Addition
				cout << x << " + " << y << " = " << x + y << endl;
				break;
	
			case '2':
				//Subtraction
				cout << x << " - " << y << " = " << x - y << endl;
				break;
			
			case '3':
				//Multiplication
				cout << x << " * " << y << " = " << x * y << endl;
				break;
	
			case '4':
				//Division
				cout << x << " / " << y << " = " << (double)x / y << endl;
				break;
	
	
			default:
				cout << "You messed up... bleh" << endl;
				break;
		}

		cout << "Would you like to do another calculation? (y/N): ";
		cin >> repeat;
	}
	while (repeat != 'n' && repeat != 'N');
}