-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConsoleCalculator.cpp
More file actions
46 lines (37 loc) · 1.26 KB
/
ConsoleCalculator.cpp
File metadata and controls
46 lines (37 loc) · 1.26 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>
int main(){
char op;
double num1, num2, result;
std::cout << "************* CALCULATOR *************" << '\n';
std::cout << "Enter either +, -, *, or /: ";
std::cin >> op;
if(op != '+' && op != '-' && op != '*' && op != '/'){
std::cout << "That was not a valid operator\n";
return 1; // Exit the program if the operator is invalid
}
std::cout << "Enter #1: ";
std::cin >> num1;
std::cout << "Enter #2: ";
std::cin >> num2;
switch(op){
case '+':
result = num1 + num2;
std::cout << "result: " << result << '\n';
break;
case '-':
result = num1 - num2;
std::cout << "result: " << result << '\n';
break;
case '*':
result = num1 * num2;
std::cout << "result: " << result << '\n';
break;
case '/':
result = num1 / num2;
std::cout << "result: " << result << '\n';
break;
//default is not needed here because we already checked for invalid operator above, if you wait to check operator till after you get input 1 and 2 it messes up the output
}
std::cout << "**************************************";
return 0;
}