Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Modify the program again so it displays a menu allowing the user to select an ad

ID: 3753879 • 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 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 <ctime>
#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;

// 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;
}

Explanation / Answer

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()
{
int num1, // The first random number
num2, // The second random number
choice, // The user's choice to solve the problem
userAnswer,

correctAnswer; // The correct answer


// Seed the random number generator.
srand(time(0));

do
{ // Display the menu and get a choice.

cout << "------------------------------ ";
cout << "1. Addition problem ";
cout << "2. Subtraction problem ";
cout << "3. Multiplication problem ";
cout << "4. Division problem ";
cout << "5. Quit this program ";
cout << "------------------------------ ";
cout << "Enter your choice (1-5): ";
cin >> choice;

// check the choice.
while (choice < 1 || choice > 5)
{
cout << "The valid choices are 1, 2, 3, "
<< "4, and 5. Please choose: ";
cin >> choice;
}

// Produce a problem.
switch (choice)
{
case 1: // Addition problem
// Generate two random numbers in
// the range 1 - 500.
num1 = 1 + rand() % 500;
num2 = 1 + rand() % 500;

// Calculate the correct answer.
correctAnswer = num1 + num2;

// Display the problem.
cout << " ";
cout << " " << setw(4) << num1 << endl;
cout << " +" << setw(4) << num2 << endl;
cout << " " << "----" << endl;
cout << " ";
break;

case 2: // Subtraction problem
// Generate two random numbers in
// the range 1 - 999.
num1 = 1 + rand() % 999;
num2 = 1 + rand() % 999;

// Make sure num2 <= num1...
while (num2 > num1)
num2 = 1 + rand() % 999;

// Get the correct answer.
correctAnswer = num1 - num2;

// Display the problem.
cout << " ";
cout << " " << setw(4) << num1 << endl;
cout << " -" << setw(4) << num2 << endl;
cout << " " << "----" << endl;
cout << " ";
break;

case 3: // Multiplication problem
// Generate two random numbers. The first in
// the range 1 - 100, the second in the
// range 1 - 9.
num1 = 1 + rand() % 100;
num2 = 1 + rand() % 9;

// Calculate the correct answer.
correctAnswer = num1 * num2;

// Display the problem.
cout << " ";
cout << " " << setw(4) << num1 << endl;
cout << " *" << setw(4) << num2 << endl;
cout << " " << "----" << endl;
cout << " ";
break;

case 4: // Division problem with no remainder
// Generate a single digit divisor.
num2 = 1 + rand() % 9;

// Generate a number that is a multiple
// of num2...
num1 = num2 * (rand() % 50 + 1);

// Calculate the correct answer.
correctAnswer = num1 / num2;

// Display the problem.
cout << " ";
cout << " " << num1 << " / " << num2 << " = ";
break;

case 5: // The user chose to quit the program.
cout << "Thank you! ";
break;
}

// If user selected a problem, get and evaluate the answer.
if (choice >= 1 && choice <= 4)
{
cin >> useranswer;
if (useranswer == correctAnswer)
cout << " Congratulations! That's right. ";
else
cout << " Sorry, the correct answer is " << correctAnswer
<< ". ";
}
} while (choice != 5); // Loop again if user did not choose to quit.
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote