Write a program that is able to compute some operations on an integer. The program writes the value of the integer and writes the following menu :
1. Add 1
2. Multiply by 2
3. Subtract 4
4. Quit
The programs ask the user to type a value between 1 and 4. If the user types a value from 1 to 3 the operation is computed, the integer is written and the menu is displayed again. If the user types 4, the program quits. Students can use any type of Control Structure Selection and Looping to answer the question.
#include <iostream>
using namespace std;
int main(){
int number;
int choice=0;
cout<<"Enter the number: ";
cin>>number;
while(choice!=4){
cout<<"1. Add 1\n";
cout<<"2. Multiply by 2\n";
cout<<"3. Subtract 4\n";
cout<<"4. Quit\n";
cout<<"Your choice: ";
cin>>choice;
//the user to type a value between 1 and 4. If the user types a value from 1 to 3 the operation is computed,
//the integer is written and the menu is displayed again.
switch(choice){
case 1:
number+=1;
cout<<"\nNumber = "<<number<<"\n\n";
break;
case 2:
number*=2;
cout<<"\nNumber = "<<number<<"\n\n";
break;
case 3:
number-=4;
cout<<"\nNumber = "<<number<<"\n\n";
break;
case 4:
// the user types 4, the program quits.
break;
default:
cout<<"\nWrong menu item\n\n";
break;
}
}
return 0;
}
Output:
Comments
Leave a comment