-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTask2.cpp
More file actions
69 lines (60 loc) · 1.74 KB
/
Copy pathTask2.cpp
File metadata and controls
69 lines (60 loc) · 1.74 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
#include <iostream>
using namespace std;
double add(double a, double b) {
return a + b;
}
double sub(double a, double b) {
return a - b;
}
double mul(double a, double b) {
return a * b;
}
double division(double a, double b) {
if (b == 0) {
throw runtime_error("Error: Division by zero");
}
return a / b;
}
int main() {
while (true) {
int opt;
double a, b;
cout << "Enter Number 1: ";
cin >> a;
cout << "Enter Number 2: ";
cin >> b;
cout << endl;
cout << "Choose an operation to perform:" << endl;
cout << "1) Addition" << endl << "2) Subtraction" << endl << "3) Multiplication" << endl << "4) Division" << endl;
cin >> opt;
try {
switch (opt) {
case 1:
cout << "Result: " << add(a, b) << endl;
break;
case 2:
cout << "Result: " << sub(a, b) << endl;
break;
case 3:
cout << "Result: " << mul(a, b) << endl;
break;
case 4:
cout << "Result: " << division(a, b) << endl;
break;
default:
cout << "Invalid option!" << endl;
}
} catch (const runtime_error& e) {
cout << e.what() << endl;
}
int op;
cout << "Do you want to use the calculator again?" << endl;
cout << "1) Yes" << endl << "2) No" << endl;
cin >> op;
if (op != 1) {
break;
}
}
cout << "Thank you for using the calculator!" << endl;
return 0;
}