-
Notifications
You must be signed in to change notification settings - Fork 5
/
calculator.cpp
49 lines (43 loc) · 1.21 KB
/
calculator.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
// Basic calculator that adds, subtracts, multiplies, and divides
#include <iostream>
using std::cin; using std::cout; using std::endl;
int main() {
char yor = 'y';
char op;
float num1, num2;
while(yor=='y'){
cin>>num1>>op>>num2;
switch(op)
{
case '+': // Adds num1 by num2
cout << num1+num2<<"\n";
break;
case '-': // Subtracts num1 by num2
cout << num1-num2<<"\n";
break;
case '*': // Multiplies num1 by num2
cout << num1*num2<<"\n";
break;
case '/': // Divides num1 by num2
if (num2 != 0)
cout << num1/num2<<"\n";
else
cout << "You cannot divide by 0!";
break;
case '%': // Returns remainder of num1 divided by num2
if (num2 != 0)
cout << (int)num1%(int)num2<<"\n";
else
cout << "You cannot divide by 0!";
break;
default:
// If the operator is other than +, -, * or /, error message is shown
cout << "Error! Operator is not correct!" << endl;
break;
}
cout<<"do you want to use calculator y/n:";
cin>>yor;
cout<<"\n";
}
return 0;
}