Based on Control Structure Selection and Control Structure Looping, answer the following questions:
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
#include <iostream>
using namespace std;
int main(){
int n, choice;
cout<<"Enter an integer: ";
cin>>n;
cout<<n<<endl;
bool flag = true;
do{
cout<<"\n1. Add 1\n2. Multiply by 2\n3. Subtract 4\n4. Quit\n";
cin>>choice;
switch(choice){
case 1: cout<<n + 1; break;
case 2: cout<<n * 2; break;
case 3: cout<<n - 4; break;
case 4: flag = false; break;
default: cout<<"Invalid choice\n";
}
}while(flag);
return 0;
}
Comments
Leave a comment