Modify the following program 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. Input Validation: If the user selects an item not on the menu, display an error message and display the menu again.

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter7: User-defined Simple Data Types, Namespaces, And The String Type
Section: Chapter Questions
Problem 7PE
icon
Related questions
icon
Concept explainers
Question

C++ Visual Studio

Modify the following program 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.

Input Validation: If the user selects an item not on the menu, display an error message and display the menu again.

Code:

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <conio.h>

using namespace std;

int main() {
srand(static_cast<unsigned int>(time(nullptr)));

while (true) {
// Generate two random numbers.
int num1 = rand() % 1000;
int num2 = rand() % 1000;

// Display the problem.
cout << "Solve the following math problem:" << endl;
cout << num1 << " + " << num2 << endl;

// Wait for user input to reveal the answer.
cout << "Press any key to reveal the answer...";
_getch(); // Wait for a key press
cout << endl;

// Display the correct solution.
int sum = num1 + num2;
cout << num1 << " + " << num2 << " = " << sum << endl;

// Ask if user wants to continue.
cout << "Do you want to try another problem? (y/n): ";
char choice = _getch();
cout << endl;

if (choice != 'y' && choice != 'Y') {
break; // Exit the loop if the user doesn't want to continue
}

// Clear the screen for the next problem
system("cls");
}

cout << "You may exit the program." << endl;

return 0;
}

 

Thank you!

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Operators
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning