Question

In: Computer Science

Math V3.0 Modify the previous version of this program again so it displays a menu allowing...

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:

  1. Declare your variables, including the correct answer
  2. Display the menu and prompt the user for their choice
  3. Make sure it is a valid choice
  4. For each possible choice:
    1. Figure out the two operands appropriately
    2. Determine and store the correct answer
    3. Display the problem (formatted nicely!)
  5. Assuming they didn't hit 5 to exit, prompt for the answer
  6. Provide feedback on the user's answer
  7. Repeat the loop as necessary

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.

Solutions

Expert Solution

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:


Related Solutions

Temperature Converter Modify the previous version of this program so that it uses a loop to...
Temperature Converter Modify the previous version of this program so that it uses a loop to display a range of temperature conversions for either Fahrenheit to Celsius or Celsius to Fahrenheit. Note: You can start with the code from the previous version, then modify it slightly after it prompts the user for the direction to convert. It will then ask the user for a starting temperature and ending temperature. Assuming they entered the lower number first (if not, tell them...
Java Math Tutor: Write a program that displays a menu as shown in the sample run....
Java Math Tutor: Write a program that displays a menu as shown in the sample run. You can enter 1, 2, 3, or 4 for choosing an addition, subtraction, multiplication, or division test. After a test is finished, the menu is redisplayed. You may choose another test or enter 5 to exit the system. Each test generates two random single-digit numbers to form a question for addition, subtraction, multiplication, or division. For a subtraction such as number1 – number2, number1...
Modify the previous program to use the Do-While Loop instead of the While Loop. This version...
Modify the previous program to use the Do-While Loop instead of the While Loop. This version of the program will ask the user if they wish to enter another name and accept a Y or N answer. Remove the "exit" requirement from before. Output: Enter the full name of a person that can serve as a reference: [user types: Bob Smith] Bob Smith is reference #1 Would you like to enter another name (Y or N)? [user types: y] Enter...
Modify the Movie List 2D program -Modify the program so it contains four columns: name, year,...
Modify the Movie List 2D program -Modify the program so it contains four columns: name, year, price and rating (G,PG,R…) -Enhance the program so it provides a find by rating function that lists all of the movies that have a specified rating def list(movie_list): if len(movie_list) == 0: print("There are no movies in the list.\n") return else: i = 1 for row in movie_list: print(str(i) + ". " + row[0] + " (" + str(row[1]) + ")") i += 1...
C++ Write a program that displays the follow menu: Geometry Calculator    1. Calculate the Area...
C++ Write a program that displays the follow menu: Geometry Calculator    1. Calculate the Area of a Circle 2. Calculate the Area of a Rectangle    3. Calculate the Area of a Triangle 4. Quit Enter your choice (1-4): If the user enters 1, the program should ask for the radius of the circle then display it's area using the following formula: area = PIr2 Use 3,14159 for PI and the radius of the circle for r. If the...
in java Write a Java Program that displays a menu with five different options: 1. Lab...
in java Write a Java Program that displays a menu with five different options: 1. Lab Test Average Calculator 2. Dice Roll 3. Circle Area Calculator 4. Compute Distance 5. Quit The program will display a menu with each of the options above, and then ask the user to enter their choice. There is also a fifth option to quit, in which case, the program will simply display a goodbye message. Based on the user’s choice, one of the options...
What is the output of the following program? Slightly modify the program so that: [Pts. 10]...
What is the output of the following program? Slightly modify the program so that: [Pts. 10] The Parent process calculates the val such a way that its value is 20 and it prints “This is parent process and val = 20”. Also, slightly modify The child process calculates the val such a way that its value is 25 and it prints “This is child process and val = 25”. int main() { int val = 15; int pid; if (pid...
"4. (Modify) Modify Program 7.14 so that the user inputs the initial set of numbers when...
"4. (Modify) Modify Program 7.14 so that the user inputs the initial set of numbers when the program runs. Have the program request the number of initial numbers to be entered." //C++ Program 7.14 as follows #include #include #include #include using namespace std; int main() { const int NUMELS = 4; int n[] = {136, 122, 109, 146}; int i; vector partnums(n, n + NUMELS); cout << "\nThe vector initially has the size of " << int(partnums.size()) << ",\n and...
Must be written in JAVA Code Modify the program you created in previous project to accomplish...
Must be written in JAVA Code Modify the program you created in previous project to accomplish the following: Support the storing of additional user information: street address (string), city( string), state (string), and 10-digit phone number (long integer, contains area code and does not include special characters such as '(', ')', or '-' Store the data in an ArrayList object Program to Modify import java.util.ArrayList; import java.util.Scanner; public class Address { String firstname; String lastname; int zipcode;    Address(String firstname,String...
Modify the AlienDirection program from this chapter so that the image is not allowed to move...
Modify the AlienDirection program from this chapter so that the image is not allowed to move out of the visible area of the window. Ignore any key that would allow this to happen. *Ask if you have any questions about the assignment I will try to clarify Textbook - JAVA FOUNDATIONS: INTRODUCTION TO PROGRAM DESIGN AND DATA STRUCTURES 5TH EDITION Starter Code Provided : import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.input.KeyEvent; import javafx.scene.paint.Color; import javafx.stage.Stage;...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT