Home / utk / cs102 / fa16 / labd / code_snippets / 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
55
56
57
58
59
60
61
62
63
64
65
/*
 * Example Lab for Lab D
 * 
 * Displays a menu and does certain actions depending on an option selected.
 * Features addition, subtraction, multiplication, division, quadratic formula.
 * 
 * Clara Nguyen
 */

#include <iostream>
#include <iomanip>

using namespace std;

int main() {
	char option, //Indicates what option to go with
		 repeat; //Indicates whether to repeat the program or not.
	int a, b, c; //Variables which may be used with any of the operations.
	do {
		cout << "Welcome! Let's calculate something today.\n\n    1 - Addition\n    2 - Subtraction\n    3 - Multiplaction\n    4 - Division\n\nChoose an option: ";
		cin >> option;
		
		switch (option) {
			case '1':
				//Addition
				cout << "x + y\nInput x: ";
				cin >> a;
				cout << "Input y: ";
				cin >> b;
				cout << endl << a << " + " << b << " = " << a + b << endl;
				break;
			case '2':
				//Subtraction
				cout << "x - y\nInput x: ";
				cin >> a;
				cout << "Input y: ";
				cin >> b;
				cout << endl << a << " + " << b << " = " << a - b << endl;
				break;
			case '3':
				//Multiplication
				cout << "x * y\nInput x: ";
				cin >> a;
				cout << "Input y: ";
				cin >> b;
				cout << endl << a << " * " << b << " = " << a * b << endl;
				break;
			case '4':
				//Division
				cout << "x * y\nInput x: ";
				cin >> a;
				cout << "Input y: ";
				cin >> b;
				cout << endl << a << " / " << b << " = " << (double)a / b << endl;
				break;
			default:
				cout << "Invalid Option" << endl;
				continue;
		}
		
		cout << "Would you like to do another calculation? (y/n): ";
		cin >> repeat;
	}
	while (repeat != 'n' && repeat != 'N');
}