In: Computer Science
Math V3.0
Modify the previous version of this program again so it displays a menu allowing the user to select 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. If a user selected an item not on the menu, display an error message and display the menu again.
Note: Start with your code from the previous chapter! For this assignment, you are extending your previous project by allowing for subtraction, multiplication, and division, as well as addition. This can be a bit tricky (hence this is worth two assignments), so be careful. Here is a basic outline of what your program should look like:
All generated numbers must be random. For addition and subtraction, the range of numbers must be between 50 and 500, like before. For multiplication, limit the numbers to be in the ranges 1-100 and 1-9. For division, generate the denominator in the range of 1-9. The numerator must be a multiple of the denominator (so there are no remainders for division!), no more than 50 times larger. You might have to think about this!
The output should look like this -- user inputs are in bold blue type:
Math Menu ------------------------------ 1. Addition problem 2. Subtraction problem 3. Multiplication problem 4. Division problem 5. Quit this program ------------------------------ Enter your choice (1-5): 4 66 / 6 = 11 Congratulations! That's right. Math Menu ------------------------------ 1. Addition problem 2. Subtraction problem 3. Multiplication problem 4. Division problem 5. Quit this program ------------------------------ Enter your choice (1-5): 2 473 - 216 = 241 Sorry! That's incorrect. Math Menu ------------------------------ 1. Addition problem 2. Subtraction problem 3. Multiplication problem 4. Division problem 5. Quit this program ------------------------------ Enter your choice (1-5): 5 Thank you for using Math.
Code :
#include<iostream>
#include<iomanip>
#include<time.h>
using namespace std;
// Function to generate Random Numbers in a range
int random(int min, int max) //range : [min, max)
{
static bool first = true;
if (first)
{
srand( time(NULL) ); //seeding for
the first time only!
first = false;
}
return min + rand() % (( max + 1 ) - min);
}
int main()
{
int choice=0,number1,number2,solution;
while(choice!=5)
{
cout<<"\n\nMath
Menu\n"<<"-----------------------\n";
cout<<"1.Addition
problem\n";
cout<<"2.Subtraction
problem\n";
cout<<"3.Multiplication
problem\n";
cout<<"4.Division
problem\n";
cout<<"5.Quit this
program\n";
cout<<"-----------------------\n";
cout<<"Enter your
choice(1-5):";
cin>>choice;
cout<<"\n";
switch(choice)
{
case 1:
number1=random(50,500);
number2=random(50,500);
cout<<"\n"<<number1<<"+"<<number2<<"=";
cin>>solution;
if(solution==(number1+number2))
cout<<"\nCongratulations! That's right\n";
else
cout<<"\nSorry! That's
incorrect\n";
break;
case 2:
number1=random(50,500);
number2=random(50,500);
cout<<"\n"<<number1<<"-"<<number2<<"=";
cin>>solution;
if(solution==(number1-number2))
cout<<"\nCongratulations! That's right\n";
else
cout<<"\nSorry! That's
incorrect\n";
break;
case 3:
number1=random(1,100);
number2=random(1,9);
cout<<"\n"<<number1<<"*"<<number2<<"=";
cin>>solution;
if(solution==(number1*number2))
cout<<"\nCongratulations! That's right\n";
else
cout<<"\nSorry! That's
incorrect\n";
break;
case 4:
number2=random(1,9);
number1=random(1,50)*number2;
cout<<"\n"<<number1<<"/"<<number2<<"=";
cin>>solution;
if(solution==(number1/number2))
cout<<"\nCongratulations! That's right\n";
else
cout<<"\nSorry! That's
incorrect\n";
break;
case 5:
cout<<"Thank you for using Math.\n";
exit(1);
default:
cout<<"You need to choose between
(1-5)\n";
break;
}
}
return 0;
}
Console Output: