-
Notifications
You must be signed in to change notification settings - Fork 0
/
calc.cpp
65 lines (51 loc) · 1.75 KB
/
calc.cpp
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
#include <iostream>
using namespace std;
#include <iomanip>
#include <string>
int main(){
bool run = true;
while(run){
cout << endl << "-------------------------------------------------------------------CALCULATOR-------------------------------------------------------------------" << endl;
double num1, num2;
cout << endl << "Enter the first number for calculation:" << endl << endl;
while(!(cin >> num1)){
cout << endl << "No number detected, please enter the first number for calculation:" << endl << endl;
cin.clear();
cin.ignore();
}
cout << endl << "Enter the second number for calculation:" << endl << endl;
while(!(cin >> num2)){
cout << endl << "No number detected, please enter the second number for calculation:" << endl << endl;
cin.clear();
cin.ignore();
}
cout << endl << "Enter an operation: +, -, /, *:" << endl << endl;
char op;
cin >> op;
switch(op){
case '+':
cout << endl << num1 << " + " << num2 << " = " << (num1 + num2) << endl;
break;
case '-':
cout << endl << num1 << " - " << num2 << " = " << (num1 - num2) << endl;
break;
case '/':
if(num2 != 0){
cout << endl << num1 << " / " << num2 << " = " << (num1 / num2) << endl;
}else{
cout << endl << "Error: can not divide by 0!" << endl;
}
break;
case '*':
cout << endl << num1 << " * " << num2 << " = " << (num1 * num2) << endl;
break;
default:
cout << endl << "Invalid selection." << endl << endl;
break;
}
string key;
cout << endl << "Enter any key to continue." << endl << endl;
cin >> key;
}
return 0;
}