forked from abhijithshaji17/cpp-mastery
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path15.cpp
More file actions
56 lines (52 loc) · 1.45 KB
/
Copy path15.cpp
File metadata and controls
56 lines (52 loc) · 1.45 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
47
48
49
50
51
52
53
54
// calculator switchcase program with class and objects
#include<iostream>
using namespace std;
class Calculator{
public:
void getInput(){
int op;
double n1,n2,ans;
cout<<"----Menu Driven Calculator----\n";
cout<<"Enter the first number: ";
cin>>n1;
cout<<"Enter the second number: ";
cin>>n2;
cout<<"Select the desired operation below:\n";
cout<<"1.Addition\n2.Subtraction\n3.Multiplication\n4.Division\n";
cin>>op;
}
void calculate(){
int op;
double n1,n2,ans;
switch(op){
case 1:
ans=n1+n2;
cout<<"Sum = "<<ans;
break;
case 2:
ans=n1-n2;
cout<<"Difference = "<<ans;
break;
case 3:
ans=n1*n2;
cout<<"Product = "<<ans;
break;
case 4:
if(n2!=0){
ans=n1/n2;
cout<<"Quotient = "<<ans;
}else{
cout<<"Error: division by zero!";
}
break;
default:
cout<<"Invalid choice.";
}
}
};
int main(){
Calculator calc;
calc.getInput();
calc.calculate();
return 0;
}