Question

Menu-driven programs will display the menu options to the user and then prompt them for a...

Menu-driven programs will display the menu options to the user and then prompt them for a menu choice. This program will display the following menu in the following format:

Calculator Options:

  1. Calculate the area of a circle
  2. Calculate the area of a rectangle
  3. Calculate the area of a triangle
  4. Calculate the area of a trapezoid
  5. Calculate the area of a sphere
  6. Exit Enter your choice (1-6)

Once the user enters a choice for the menu, the program should use a switch statement to determine the user choice and calculate the area of the shape selected.

If the shape selected is a circle, the program must prompt the user for the radius of the circle. The area will be calculated and then displays the area indicating it is a circle.

For a menu choice for the rectangle, the program must prompt the user for the length and width of the rectangle. The program will then calculate the area of the rectangle and display the area indicating it is the area of a rectangle.

If the menu choice is a triangle, the program must prompt the user for the length of the triangle’s base and its height. The area is then calculated for the triangle and displays the area.

If the choice is a trapezoid, the program must prompt the user for the height and the two base lengths. The area is calculated and displayed indicating it is a trapezoid.

If a sphere is selected, the program must prompt the user for the radius. Calculate the area and then display the area of the sphere indicating it is a sphere.

Other requirements and formulas:

  • Make PI a named constant
  • Circle area = PI * r2
  • Rectangle area = length * width
  • Triangle area = base * height * 0.5
  • Trapezoid area = height/2 * (b1 +b2)
  • Sphere area = 4 * PI *r2

The program should validate the user menu choice. If an invalid menu option, it should display a message telling the user what the valid choices are and exit the program.

The program should also not allow the user to enter a negative number for any of the values used to calculate the areas. If they do the program must indicate which value entered was invalid, being specific. For example, for a rectangle, if the length is invalid there should be a message like “Value entered for the length is invalid”.

Pseudocode must be provided in the comment block at the top of the file.

C++ Language Only

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>

using namespace std;

int main()

{

    int choice;

    char ans='y';

    float const pi=3.14;

    float radius,length,width,height,base,base1,base2,area;

    do          //loop to repeatedly display the menu till the user wants to exit

    {

        //To display the menu

        cout<<"Enter your shape 1. Circle 2. Rectangle   3. Triangle 4. Trapezoid 5. Sphere 6. Exit";                     

        cin>>choice;

        switch(choice)

        {

//To calculate the area of the circle

            case 1: cout<<"\nEnter the radius";

                    cin>>radius;

                    if (radius<0)                  //checking for data validity

                    {

                      cout<<"Invalid data";

                     break;

                    }

                    area=pi*radius*radius;   

                    cout<<"Area of circle:"<<area;

                    break;

//To calculate the area of the rectangle

            case 2: cout<<"\nEnter the length and width of the rectangle";

                  cin>>length>>width;

                    if ( length<0 || width<0) //checking for data validity

                    {

                        cout<<"Invalid data";

                        break;

                    }

                    area=length*width;

                    cout<<"Area of rectangle:"<<area;

                    break;

//To calculate the area of the triangle

            case 3: cout<<"\nEnter the height and base of the Triangle";

                    cin>>height>>base;

                    if ( height<0 ||base<0)             //checking for data validity

                    {

                        cout<<"Invalid data";

                        break;

                    }

                    area=height*base*0.5;       

                    cout<<"Area of Triangle:"<<area;

                    break;

//To calculate the area of the Trapezoid

            case 4: cout<<"\nEnter the height and two base lengths of the Trapezoid";

                    cin>>height>>base1>>base2;

                    if ( height<0 || base1<0 || base2<0)           //checking for data validity

                    {

                        cout<<"Invalid data";

                        break;

                    }

                    area=height*0.5*(base1+base2);       

                    cout<<"Area of Trapezoid:"<<area;

                    break;

//To calculate the area of the sphere

            case 5: cout<<"\nEnter the radius";

                    cin>>radius;

                    if (radius<0) //checking for data validity

                    {

                        cout<<"Invalid data";

                        break;

                    }

                    area=4*pi*radius*radius;

                    cout<<"Area of sphere:"<<area;

                    break;

// To exit from the menu

            case 6: break;

//Checking for invalid choice

            default: cout<<"invalid choice. Enter a valid choice";

        }

        cout<<"Do you want to continue(y/n)";

        cin>>ans;

    }while(ans=='y');

    return 0;

}

o/p

Enter your shape 1. Circle  2. Rectangle   3. Triangle  4. Trapezoid  5. Sphere 6. Exit2                                         

                                                                                                                                 

Enter the length and width of the rectangle2                                                                                     

3                                                                                                                                

Area of rectangle:6Do you want to continue(y/n)y                                                                                 

Enter your shape 1. Circle  2. Rectangle   3. Triangle  4. Trapezoid  5. Sphere 6. Exit3                                         

                                                                                                                                 

Enter the height and base of the Triangle -8 5                                                                                    

Invalid dataDo you want to continue(y/n) y                                                                                        

Enter your shape 1. Circle  2. Rectangle   3. Triangle  4. Trapezoid  5. Sphere 6. Exit3                                         

                                                                                                                                 

Enter the height and base of the Triangle3                                                                                       

5                                                                                                                                

Area of Triangle:7.5 Do you want to continue(y/n)y                                                                                

Enter your shape 1. Circle  2. Rectangle   3. Triangle  4. Trapezoid  5. Sphere 6. Exit5                                         

                                                                                                                                 

Enter the radius3                                                                                                                

Area of sphere:113.04 Do you want to continue(y/n)y                                                                               

Enter your shape 1. Circle  2. Rectangle   3. Triangle  4. Trapezoid  5. Sphere 6. Exit 8                                         

invalid choice. Enter a valid choice Do you want to continue(y/n)y                                                                

Enter your shape 1. Circle  2. Rectangle   3. Triangle  4. Trapezoid  5. Sphere 6. Exit 6                                         

Do you want to continue(y/n) n

Add a comment
Know the answer?
Add Answer to:
Menu-driven programs will display the menu options to the user and then prompt them for a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Create a menu-driven program (using the switch) that finds and displays areas of 3 different objects....

    Create a menu-driven program (using the switch) that finds and displays areas of 3 different objects. The menu should have the following 4 choices: 1 -- rectangle 2 -- circle 3 -- triangle 4 -- quit If the user selects choice 1, the program should find the area of a rectangle. rectangle area = length * width If the user selects choice 2, the program should find the area of a circle. circle area = PI * radius * radius...

  • Geometric calclator use python to do this program. Write a program that displays the following menu:...

    Geometric calclator use python to do this program. Write a program that displays the following menu: 1. Calculate the area of circle 2. calculate the area of rectangle 3. calculate the area of triangle 4. Quit Enter your choice (1-4). if the user enters 1, your program should ask for the radius of the circle and then display its area. Use the formula to calculate the circle's area: Area = pi*r^2 Use 3.14149 for Pi and the radius of the...

  • Using C++, Write a program that will provide the user a menu from which the user...

    Using C++, Write a program that will provide the user a menu from which the user may select to calculate the area of one of four geometric shapes: a circle, a rectangle, a triangle, and a trapezoid. You should use the most appropriate SWITCH block for this program. The user will input all data needed to calculate the area. The program should output all data input by the user, the calculated area, and the geometric shape selected. Run this program...

  • Java only please Write a program that displays the following menu: Geometry Calculator 1.       Calculate the...

    Java only please Write a program that displays the following menu: Geometry Calculator 1.       Calculate the Area of a Circle 2.       Calculate the Area of a Triangle 3.     Calculate the Area of a Rectangle 4.       Quit Enter your choice (1-4): If the user enters 1, the program should ask for the radius of the circle and then display its area. Use the formula:      area = ∏r2    Use 3.14159 for ∏. If the user enters 2 the program should ask for...

  • (PYTHON) Write a program that displays the following menu:. , Write Algorithm for Code Geometry Calculator...

    (PYTHON) Write a program that displays the following menu:. , Write Algorithm for Code 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 and then display its area. If the user enters 2, the program should ask for the length and width of...

  • C++ programming For this assignment, write a program that will act as a geometry calculator. The...

    C++ programming For this assignment, write a program that will act as a geometry calculator. The program will be menu-driven and should continue to execute as long as the user wants to continue. Basic Program Logic The program should start by displaying a menu similar to the following: Geometry Calculator 1. Calculate the area of a circle 2. Calculate the area of a triangle 3. Quit Enter your choice(1-3): and get the user's choice as an integer. After the choice...

  • Design program so that it correctly meets the program specifications given below.   Specifications: Create a menu-driven...

    Design program so that it correctly meets the program specifications given below.   Specifications: Create a menu-driven program that finds and displays areas of 3 different objects. The menu should have the following 4 choices: 1 -- square 2 -- circle 3 -- right triangle 4 -- quit If the user selects choice 1, the program should find the area of a square. If the user selects choice 2, the program should find the area of a circle. If the user...

  • extra credit 1 Write a program that will display the following menu and prompt the user...

    extra credit 1 Write a program that will display the following menu and prompt the user for a selection: A) Add two numbers B) Subtract two numbers C) Multiply two numbers D) Divide two numbers X) Exit program The program should: display a hello message before presenting the menu accept both lower-case and upper-case selections for each of the menu choices display an error message if an invalid selection was entered (e.g. user enters E) prompt user for two numbers...

  • Q2: Write a menu-driven C program using switch-case to calculate the following: 1. Area of circle...

    Q2: Write a menu-driven C program using switch-case to calculate the following: 1. Area of circle 2. Area of square 3. Area of rectangle The program should use the following functions properly: void displayMenu() //a function that will display the menu options to the user int getChoice() //a function that will input the user choice and returns it float calculate(int choice) //a function that reads the required inputs based on the user choice, and returns the area of the shape...

  • Problem: Given information about a circle, rectangle and a triangle, calculate the area of the shape...

    Problem: Given information about a circle, rectangle and a triangle, calculate the area of the shape from the information supplied. Write pseudocode to solve this problem and write a Python program that asks the user to input the required information for a shape and then calculates the area of the shape. 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...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT