Modify the program again so it displays a menu allowing the user to select an ad
ID: 3529481 • Letter: M
Question
Modify the program again so it displays a menu allowing the user to select an addition, subtraction, multiplication, or division problem. The final selection on the menu should let the user quit the program. After the user has finshed the math problem, the program should display the menu again. This process is repeated until the user chooses to quit the program. Input Validation: If the user selects an item not on the menu, display an error message and display the menu again #include #include #include #include #include using namespace std; int main() { // presenting menu to screen cout << "Menu "; cout << " 1. Addition "; cin.get(); cout << " 2. Subtraction "; cin.get(); cout << "3. Multiplication"; cin.get(); cout << "4. Divison "; cin.get(); cout << "Quit"; cin.get(); cout << "Please press enter... "; cin.get(); cout << "Please enter 1 for Addition, 2 for Subtraction, 3 Multiplication, " << "4 for Division, " << "Any other number to quit. "; int programChoice; // variable for which program the user wants to run cin >> programChoice; // gathering data from user switch ( programChoice ) int choice; // menu choice //This program takes two random numbers and asks the user to add them together. // program goals cout << "This program will ask you for the sum of two randomly chosen numbers, " << "Then will display a positive or negative message depending on if you are right or wrong. "; int quit = 0; // variable allowing the user to quit do { int num1, num2, sum, answer; //numbers to add, the correct sum and the user's answer. //generating ramndom numbers, 1 - 1000 unsigned seed = time(0); srand(seed); num1 = 1 + rand() % 1000; num2 = 1 + rand() % 1000; sum = num1 + num2; // adding the two random numbers cout << "What is the sum? "; cout << " " << num1 << " " // aligning the numbers properly << "+ "; if ( num2 < 100 ) cout << " " << num2 << " "; else cout << num2 << " "; cout << "------------- "; if ( sum < 1000 ) cout << " "; cin >> answer; // user's answer // declaring if the user's answer is correct if ( answer == sum ) cout << "Good job!!! you are correctumundo!! "; else cout << "Ohhhhh....too bad...better luck next time! "; cout << "Press 1 to quit, anything else to continue... "; cin >> quit; } while ( quit != 1); } break; return 0; }Explanation / Answer
#include<iostream>
#include<ctime>
#include<cstdlib>
using namespace std;
int main()
{
int num1, num2, answer;
while(1)
{
cout << " Menu ";
cout << " 1. Addition ";
cout << " 2. Subtraction ";
cout << " 3. Multiplication ";
cout << " 4. Divison ";
cout << " 5. Quit ";
cout << " Please enter 1 for Addition, 2 for Subtraction, 3 Multiplication, "
<< "4 for Division, "
<< "Any other number to quit. ";
int programChoice;
cin >> programChoice;
if(programChoice==1 || programChoice==2 || programChoice==3 || programChoice==4)
{
cout<<" Enter The first number";
cin>>num1;
cout<<" Enter The second number";
cin>>num2;
}
switch ( programChoice )
{
case 1: answer=num1+num2;
break;
case 2: answer=num1-num2;
break;
case 3: answer=num1*num2;
break;
case 4: if(num1==0)
{
cout<<" Division not possible";
return 0;
}
answer=num1/num2;
break;
case 5: exit(0);
break;
default: cout<<" Invalid number.";
}
cout<<" Answer is:"<<answer;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.