Write a menu-driven C++ program that performs the following according to user’s choice: 1. Add 2. Subtract 3. Multiply 4. Quit

Write a menu-driven C++ program that performs the following according to user’s choice:
Add
Subtract
Multiply
Quit
The program shall ask user to choose an option between 1 and 4, and then perform the desired calculation. For instance, if user specifies 1, then the program requests from the user two values, calculates addition of them, and finally displays the result obtained to the screen. Hint: Use switch-case statements for the menu!
Solution :
Input Screen #include
include
using namespace std;
int main()
{
char ch;
int num1, num2,total;
//we use while loop and it closed when we press 4 "Quit"
while (true)
{
cout << "\n……………"; cout << "\n1. Add " "\n2. Subtract " "\n3. Multiply " "\n4. Quit \n" ; cout << "\n Please choose your option: \n"; cin >> ch;
switch (ch)
{
//this case we use for add two numbers
case '1':
cout << "Enter two numbers: \n"; cin >> num1 >> num2;
total = num1 + num2;
cout << "Sum = " << total; break; //this case we use for subtract two numbers case '2': cout << "Enter two numbers: \n"; cin >> num1 >> num2;
total = num1 - num2;
cout << "Subtraction = " << total; 5 break; case '3': //this case we use for multiply two numbers cout << "Enter two numbers: \n"; cin >> num1 >> num2;
total = num1 * num2;
cout << "Product = " << total;
break;
case '4':
//this case we use for exit from program
cout << "Exit …" << endl;
exit(0);
default:
cout << "Wrong choice " << endl;
}
}
return 0;
}

Output Screen

Leave a Comment

Your email address will not be published. Required fields are marked *