Answer to Question #189202 in C++ for Skye Jordan

Question #189202

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.


1
Expert's answer
2021-05-06T04:30:57-0400
#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:


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS