-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalcu.cpp
More file actions
58 lines (50 loc) · 1.16 KB
/
calcu.cpp
File metadata and controls
58 lines (50 loc) · 1.16 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
#include <iostream>
using namespace std;
int add(int x, int y) {
return x + y;
}
int sub(int x, int y) {
return x - y;
}
int divide(int x, int y) {
if (y == 0) {
cout << "Invalid! Not divisible by zero" << endl;
return 0; // Return a default value
}
else {
return x / y;
}
}
int multiply(int x, int y) {
return x * y;
}
int main() {
int sum;
int num;
char oper;
cout << "This is a calculator.\n" << endl;
cin >> sum;
// Loop until an invalid operator is entered
while (cin >> oper && (oper == '+' || oper == '-' || oper == '*' || oper == '/')) {
cin >> num;
switch (oper) {
case '+':
sum = add(sum, num);
break;
case '-':
sum = sub(sum, num);
break;
case '*':
sum = multiply(sum, num);
break;
case '/':
sum = divide(sum, num);
break;
default:
cout << "Invalid operator!" << endl;
break;
}
}
cout << "Your answer: " << sum << endl;
return 0;
}