-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculator.cpp
124 lines (114 loc) · 4.45 KB
/
calculator.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <iostream>
#include <limits>
using namespace std;
// Function to clear the input stream in case of an error
void clearInput() {
cin.clear(); // clear the error flag
cin.ignore(numeric_limits<streamsize>::max(), '\n'); // discard bad input
}
// Function to display the calculator menu with colored text
void displayMenu() {
cout << "\033[1;39m" << "MADE BY " << endl;
cout << "\033[1;39m" << " ⤡ " << endl;
cout << "\033[1;39m" << " \033[1;36m GET UNKNOWN ERROR "<<"\033[1;33m" << "v2.7" << endl;
cout << "\033[1;31m" << "===========================" << endl;
cout << "\033[1;32m" << " Advanced Calculator" << endl;
cout << "\033[1;31m" << "===========================" << endl;
cout << "\033[0m"; // Reset color
cout << "\033[1;36m" << "Choose an operation:" << endl;
cout << "1. Addition (+)" << endl;
cout << "2. Subtraction (-)" << endl;
cout << "3. Multiplication (*)" << endl;
cout << "4. Division (/)" << endl;
cout << "5. Modulus (%)" << endl;
cout << "6. Exit" << "\033[0m" << endl;
cout << "\033[1;33m" << "Enter your choice: " << "\033[0m";
}
// Function to get two numbers from the user
bool getTwoNumbers(double &num1, double &num2) {
cout << "Enter First Number: ";
if (!(cin >> num1)) {
cout << "\033[1;31m" << "Invalid input. Please enter numeric values." << "\033[0m" << endl;
clearInput();
return false;
}
cout << "Enter Second Number: ";
if (!(cin >> num2)) {
cout << "\033[1;31m" << "Invalid input. Please enter numeric values." << "\033[0m" << endl;
clearInput();
return false;
}
return true;
}
int main() {
int choice;
double num1, num2, result;
bool keepRunning = true;
while (keepRunning) {
displayMenu();
cin >> choice;
if (cin.fail()) {
cout << "\033[1;31m" << "**Unknown error occurred!** Please enter valid choice." << "\033[0m" << endl;
clearInput();
continue;
}
switch (choice) {
case 1: // Addition
if (getTwoNumbers(num1, num2)) {
result = num1 + num2;
cout << "\033[1;32m" << "Result: " << result << "\033[0m" << endl;
}
break;
case 2: // Subtraction
if (getTwoNumbers(num1, num2)) {
result = num1 - num2;
cout << "\033[1;32m" << "Result: " << result << "\033[0m" << endl;
}
break;
case 3: // Multiplication
if (getTwoNumbers(num1, num2)) {
result = num1 * num2;
cout << "\033[1;32m" << "Result: " << result << "\033[0m" << endl;
}
break;
case 4: // Division
if (getTwoNumbers(num1, num2)) {
if (num2 != 0) {
result = num1 / num2;
cout << "\033[1;32m" << "Result: " << result << "\033[0m" << endl;
} else {
cout << "\033[1;31m" << "Error: Division by zero!" << "\033[0m" << endl;
}
}
break;
case 5: // Modulus
cout << "Enter First Number: ";
int int1, int2;
if (cin >> int1) {
cout << "Enter Second Number: ";
if (cin >> int2) {
if (int2 != 0) {
result = int1 % int2;
cout << "\033[1;32m" << "Result: " << result << "\033[0m" << endl;
} else {
cout << "\033[1;31m" << "Error: Division by zero!" << "\033[0m" << endl;
}
} else {
cout << "\033[1;31m" << "Invalid input. Please enter integer values." << "\033[0m" << endl;
clearInput();
}
} else {
cout << "\033[1;31m" << "Invalid input. Please enter integer values." << "\033[0m" << endl;
clearInput();
}
break;
case 6: // Exit
keepRunning = false;
break;
default:
cout << "\033[1;31m" << "**Unknown error occurred!**" << "\033[0m" << endl;
break;
}
}
return 0;
}