Calculator using C++

No comments

#include<iostream>
#include<conio.h>
using namespace std;

class calculator
{
int a;
int b;

public:
void input();
int add();
int subtract();
int multiply();
int divide();
int modulus();
};


void calculator::input()
{
cout<<"enter a number:";
cin>>a;
cout<<("enter another number:");
cin>>b;
}
int calculator::add()
{
cout<<a<<" + "<<b<<" = "<<a + b<<endl;
return a + b;
}

int calculator::subtract()
{
cout<<a<<" - "<<b<<" = "<<a - b<<endl;
return a - b;
}
int calculator::multiply()
{
cout<<a<<" * "<<b<<" = "<<a * b<<endl;
return a * b;
}

int calculator::divide()
{
if(b)
{
cout<<a<<" / "<<b<<" = "<<a / b<<endl;
return a / b;
}
cout<<"can not divide by"<<b<<endl;
return 0;
}

int calculator::modulus()
{
if(b)
{
cout<<a<<" mod "<<b<<" = "<<a % b<<endl;
return 1;
}
cout<<"can not divide by"<<b<<endl;
return 0;
}

int show_menu()
{
cout<<"1. input\n";
cout<<"2. add\n";
cout<<"3. subtract\n";
cout<<"4. divide\n";
cout<<"5. multiply\n";
cout<<"6. modulus\n";
cout<<"7. exit\n";
cout<<"Enter choice:\n";
int x;
cin>>x;
return x;

}
void main()
{
int choice;
calculator c;
do{
choice = show_menu();

switch(choice)
{
case 1:
c.input();
break;
case 2:
c.add();
break;
case 3:
c.subtract();
break;
case 4:
c.divide();
break;
case 5:
c.multiply();
break;
case 6:
c.modulus();
break;
case 7:
break;
default:
cout<<"\n\ninvalid choice.\n\n";

break;
}
getch();
}while(choice != 0);
}

No comments :

Post a Comment