-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModule3LabWeek2Calculator.cpp
More file actions
123 lines (94 loc) · 4.13 KB
/
Copy pathModule3LabWeek2Calculator.cpp
File metadata and controls
123 lines (94 loc) · 4.13 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
//************************************************************************
// Author: Rolando Rey Carreon
// Date: Mar 7 2025
// Language: C++
// Assignment: Module 3 Lab Week 2 Calculator
// Description: In this lab i will writee a c++ program that implements a
// simple calculator using a switch statement.
//************************************************************************
#include<iostream>
using namespace std;
int main()
{
// declare local variables
int menuChoice;
int valueOne, valueTwo;
int result;
// prompt menu that asks the user to choose a listed choice,
// keeps reprompting on valid and invalid choice till exit '0'
do {
//display menu and get the user choice
cout << "Calculator Menu:\n"
<< "1. Addition\n"
<< "2. Subtraction\n"
<< "3. Multiplication\n"
<< "4. Division\n"
<< "0. Exit\n"
<< "Enter your choice: ";
cin >> menuChoice;
// switch containing 5 menu items and default
switch(menuChoice)
{
// menu choice 1 prompts for two values to add
case 1:
cout << "Enter the first number: ";
cin >> valueOne;
cout << "Enter the second number: ";
cin >> valueTwo;
// addition formula saves to result int
result = valueOne + valueTwo;
// displays result and reprompt menu
cout << "Result: " << result << endl << endl;
break;
// menu choice 2 prompts for two values to subtract
case 2:
cout << "Enter the first number: ";
cin >> valueOne;
cout << "Enter the second number: ";
cin >> valueTwo;
// subtraction formula saves to result int
result = valueOne - valueTwo;
// displays result and reprompt menu
cout << "Result: " << result << endl << endl;
break;
// menu choice 3 prompts for two values to multiply
case 3:
cout << "Enter the first number: ";
cin >> valueOne;
cout << "Enter the second number: ";
cin >> valueTwo;
// multiplication formula saves to result int
result = valueOne * valueTwo;
// displays result and reprompt menu
cout << "Result: " << result << endl << endl;
break;
// menu choice 4 prompts for two values to divide
case 4:
cout << "Enter the first number: ";
cin >> valueOne;
cout << "Enter the second number: ";
cin >> valueTwo;
// division formula saves to result int
result = valueOne / valueTwo;
// if division result is 0 display error and reprompt menu
// else display the result and reprompt menu
if ( result == 0 )
{
cout << "Division by Zero is undefined.\n\n";
} else
cout << "Result: " << result << endl << endl;
break;
// menu choice 0 displays exit message and ends program
case 0:
cout << "Exiting the calculator. Goodebye!\n";
break;
// any not listed choice will display as invalid and reprompt
default:
cout << "that is not a valid option.\n\n";
break;
} // end of switch
} while (menuChoice != 0); // reprompts until user selects 0 to quit
// padding
cout << endl << endl;
return 0;
}