forked from cpp-tutor/learnmoderncpp-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path03-calc.cpp
More file actions
34 lines (32 loc) · 676 Bytes
/
03-calc.cpp
File metadata and controls
34 lines (32 loc) · 676 Bytes
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
// 03-calc.cpp : simple calculator with four functions
import std;
using namespace std;
int main() {
int r{}, x{}, y{};
char op{};
cout << "Please enter a calculation (number op number, op is one of +-*/):\n";
cin >> x >> op >> y;
switch (op) {
case '+':
r = x + y;
break;
case '-':
r = x - y;
break;
case '*':
r = x * y;
break;
case '/':
if (y) {
r = x / y;
}
else {
cerr << "Error: divide by zero.\n";
}
break;
default:
cerr << "Error: invalid op.\n";
break;
}
cout << "Result: " << r << '\n';
}