-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
103 lines (93 loc) · 3.08 KB
/
main.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
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
#include <iostream>
#include <cmath>
void displayMenu() {
std::cout << "=====================\n";
std::cout << " Calculator \n";
std::cout << "=====================\n";
std::cout << "Select an operation\n";
std::cout << "1. Addition (+)\n";
std::cout << "2. Subtraction (-)\n";
std::cout << "3. Multiplication (*)\n";
std::cout << "4. Division (/)\n";
std::cout << "5. Modulus (%)\n";
std::cout << "6. Exponentation (^)\n";
std::cout << "7. Square Root (√)\n";
std::cout << "8. Exit\n";
}
int main() {
double num1, num2, result;
char op;
int choice;
while (true) {
// Display the menu
displayMenu();
// Prompt user to select an operation
std::cout << "Enter your choice (1-8): ";
std::cin >> choice;
// Exit prompt if user chooses 5
if (choice == 8) {
std::cout << "Exiting the calculator. Goodbye" << std::endl;
break;
}
// Check for valid choice
if (choice < 1 || choice > 7) {
std::cout << "Invalid choice option. Please select a number 1-8." << std::endl;
continue;
}
// Prompt user for the numbers
if (choice != 7) {
std::cout << "Enter the 2 numbers you would like to perform an operation on. Enter them in the order you want operated (e.g., 5 * 3, 5 comes first).";
std::cin >> num1 >> num2;
std::cout << std::endl;
} else {
std::cout << "Enter the number you want to find the square root of: ";
std::cin >> num1;
std::cout << std::endl;
}
switch(choice) {
case 1:
op = '+';
result = num1 + num2;
break;
case 2:
op = '-';
result = num1 - num2;
break;
case 3:
op = '*';
result = num1 * num2;
break;
case 4:
op = '/';
if (num2 != 0) {
result = num1 / num2;
} else {
std::cout << "Division by 0 is not allowed" << std::endl;
continue;
}
break;
case 5:
op = '%';
result = static_cast<int>(num1) % static_cast<int>(num2);
break;
case 6:
op = '^';
result = std::pow(num1, num2);
std::cout << num1 << "^" << num2 << " = " << result << std::endl;
continue;
case 7:
op = '√';
result = std::sqrt(num1);
std::cout << "sqrt(" << num1 << ")" << " = " << result << std::endl;
continue;
default:
std::cout << "Error: Invalid operator" << std::endl;
continue;
}
// Display the result
std::cout << num1 << " " << op << " " << num2 << " = " << result << std::endl;
std::cout << std::endl;
std::cout << std::endl;
}
return 0;
}