Mar 4, 2013 at 4:46am UTC
TASK: Write a 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 program. After the user has finished math problem, the program should display the menu again.
This is modifying a prior program we used that assigns random problems to be answered and shows the solution if the answer isn't right.
My problem with the quitting process as "break;" statement turned out not to be enough for me to break out of the loop. Thanks for your help.
Code:
#include <cstdlib>
#include <iostream>
#include <ctime>
#include <cmath>
using namespace std;
int main ()
{
// random number generator
srand(time(NULL));
// start the infinite loop
while (true)
{
// prompt user for menu
cout << "Math Tutor" << endl;
cout << "1. Addition" << endl;
cout << "2. Subtraction" << endl;
cout << "3. Multiplication" << endl;
cout << "4. Division" << endl;
cout << "5. Quit" << endl;
cout << "Enter your choice (1-5): ";
int choice;
cin >> choice;
cin.ignore (1000, 10);
// menu options
// addition
if (choice == 1)
{
// declare variables and give random values for them
int num1 = rand() % 1000 + 1;
cout << num1 << endl;
int num2 = rand() % 1000 + 1;
cout << num2 << endl;
cout << " -----" << endl;
int answer;
cin >> answer;
cin.ignore(1000, 10);
int sum = num1 + num2;
// pause for user input
if (answer == sum)
cout << "Congratulations, you got the right answer!" << endl;
else
{
// show the solution
cout << endl;
cout << "Sorry, that was wrong :(. Correct solution is" << endl;
cout << num1 << endl;
cout << num2 << endl;
cout << " -----" << endl;
cout << sum << endl;
}
cout << endl;
}
// subtraction
else if (choice == 2)
{
// declare variables and give random values for them
int num1 = rand() % 1000 + 1;
cout << num1 << endl;
int num2 = rand() % 1000 + 1;
cout << num2 << endl;
cout << " -----" << endl;
int answer;
cin >> answer;
cin.ignore(1000, 10);
int sum = num1 - num2;
// pause for user input
if (answer == sum)
cout << "Congratulations, you got the right answer!" << endl;
else
{
// show the solution
cout << endl;
cout << "Sorry, that was wrong :(. Correct solution is" << endl;
cout << num1 << endl;
cout << num2 << endl;
cout << " -----" << endl;
cout << sum << endl;
}
cout << endl;
}
// multiplication
else if (choice == 3)
{
// declare variables and give random values for them
int num1 = rand() % 1000 + 1;
cout << num1 << endl;
int num2 = rand() % 1000 + 1;
cout << num2 << endl;
cout << " -----" << endl;
int answer;
cin >> answer;
cin.ignore(1000, 10);
int sum = num1 * num2;
// pause for user input
if (answer == sum)
cout << "Congratulations, you got the right answer!" << endl;
else
{
// show the solution
cout << endl;
cout << "Sorry, that was wrong :(. Correct solution is" << endl;
cout << num1 << endl;
cout << num2 << endl;
cout << " -----" << endl;
cout << sum << endl;
}
cout << endl;
}
// division
else if (choice == 4)
{
// declare variables and give random values for them
int num1 = rand() % 1000 + 1;
cout << num1 << endl;
int num2 = rand() % 1000 + 1;
cout << num2 << endl;
cout << " -----" << endl;
int answer;
cin >> answer;
cin.ignore(1000, 10);
int sum = num1 / num2;
// pause for user input
if (answer == sum)
cout << "Congratulations, you got the right answer!" << endl;
else
{
// show the solution
cout << endl;
cout << "Sorry, that was wrong :(. Correct solution is" << endl;
cout << num1 << endl;
cout << num2 << endl;
cout << " -----" << endl;
cout << sum << endl;
}
cout << endl;
}
// quit
else if (choice == 5)
break;
}
}