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
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 finished the math problem, the program should display the menu again. This process is repeated until the user chooses to quit the program.
Use a switch statement for the menu
Input Validation: If the user selects an item not on the menu, display an error message and display the menu again.
Addition: Choose 2 random numbers. Top number between 1 and 500. Bottom number less than top number and between 1 and 99.
Subtraction: Choose 2 random numbers between 1 and 500. The bottom number is less than top.
Multiplication: Choose 2 random numbers between 1 and 12.
Division: No remainder. Choose a single digit number for number 2. Number 1 should be a 3 digit multiple of number 2.
#include
// Declare function prototypes int addition(int, int); int subtraction(int, int); int multiplication(int, int);
int main() { srand(time(0)); // Feed random generator int min, max, num1, num2, answer, Choice, result; { cout << "1: Addition. " << "2: Subtraction. " << "3: Multiplication. " << "4: Exit. " << " Enter your choice: "; cin >> Choice; // Read choice
if (Choice == 1) // If user choice is 1 then addition { min = 1; max = 100; num1 = (rand() % (max - min + 1)) + min; min = 1; max = 10; num2 = (rand() % (max - min + 1)) + min;
result = addition(num1, num2); // Addition result // Ask the user for answer cout << " What is the answer for " << num1 << " + " << num2 << " = "; cin >> answer; if (answer == result) cout << "Congratulations! Good Job. " << endl; else cout << "That is not correct. " << endl; }
else if (Choice == 2) // If user choice is 2, then subtraction { min = 1; max = 100; num1 = (rand() % (max - min + 1)) - min; min = 1; max = 10; num2 = (rand() % (max - min + 1)) + min;
result = subtraction(num1, num2); // Subtraction result // Ask for answer from user cout << " What is the answer for " << num1 << " - " << num2 << " = "; cin >> answer; if (answer == result) cout << "Congratulations! Good Job. " << endl; else cout << "That is not correct. " << endl; }
else if (Choice == 3) { min = 1; max = 12; num1 = (rand() % (max - min + 1)) + min; num2 = (rand() % (max - min + 1)) + min; // Multiplication result result = multiplication(num1, num2); // Ask the user for the answer cout << " What is the answer for " << num1 << " * " << num2 << " = "; cin >> answer; if (answer == result) cout << "Congratulations! Good Job. " << endl; else cout << "That is not correct. " << endl; } else if (Choice == 4) { cout << " Goodbye! "; return 0; }
else { cout << " Invalid choice. Please try again. "; } }
system("pause"); // keeps the console window open }
int addition(int num1, int num2) { int result; // Add result = num1 + num2; return result; }
int subtraction(int num1, int num2) { int result; // Subtract result = num1 - num2; return result; }
int multiplication(int num1, int num2) { int result; // Multiply result = num1 * num2; return result; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started