Write a program in C++ that will simulate a basic calculator. The program should present a menu to the user and ask the user to type an option. You should use the switch statement to execute the operation selected. The menu should have the following options:
1. Add
2. Multiply
3.Subtract
4.Divide
The program should then ask for 2 numbers and perform the operation. For the 'Subtract' option, the program should check to see which number is greater then subtract the smaller number from the larger.
#include <iostream>
using namespace std;
int main(){
double number1,number2;
int choice=0;
while(choice!=5){
cout<<"1. Add\n";
cout<<"2. Multiply\n";
cout<<"3. Subtract\n";
cout<<"3. Divide\n";
cout<<"4. Quit\n";
cout<<"Your choice: ";
cin>>choice;
if(choice>=1 && choice<=3){
cout<<"Enter the number 1: ";
cin>>number1;
cout<<"Enter the number 2: ";
cin>>number2;
}
switch(choice){
case 1:{
double sum = number1 + number2;
cout<<"\nSum = "<<sum<<"\n\n";
}
break;
case 2:{
double product = number1 * number2;
cout<<"\nProduct = "<<product<<"\n\n";
}
break;
case 3:{
double difference = number1 - number2;
if(number1 < number2){
difference = number2 - number1;
}
cout<<"\nDifference = "<<difference<<"\n\n";
}
break;
case 4:{
double quotient = number1 / number2;
cout<<"\nQuotient = "<<quotient<<"\n\n";
}
break;
case 5:
break;
default:
cout<<"\nWrong menu item\n\n";
break;
}
}
return 0;
}
Comments
Leave a comment