generated from ucsb-cs16-f24/github-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalculator.cpp
More file actions
48 lines (39 loc) · 1.04 KB
/
calculator.cpp
File metadata and controls
48 lines (39 loc) · 1.04 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
#include <iostream>
using namespace std;
int main() {
char operation;
double num1, num2, result;
cout << "Simple Calculator" << endl;
cout << "Enter an operation (+, -, *, /): ";
cin >> operation;
cout << "Enter the first number: ";
cin >> num1;
cout << "Enter the second number: ";
cin >> num2;
switch (operation) {
case '+':
result = num1 + num2;
cout << "Result: " << result << endl;
break;
case '-':
result = num1 - num2;
cout << "Result: " << result << endl;
break;
case '*':
result = num1 * num2;
cout << "Result: " << result << endl;
break;
case '/':
if (num2 ==0){
cerr << "Error: Division by 0!" << endl;
exit(1);
}
result = num1 / num2; // No check for dividing by 0
cout << "Result: " << result << endl;
break;
default:
cout << "Invalid operation!" << endl;
break;
}
return 0;
}