Question

In: Computer Science

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 user enters 2, the program should ask for the length and width of the rectangle, then display the rectangle's area. Use the following formula: area = length * width

If the user enters 3, the program should ask for the length of the triangle's base and it's height, the display its area. Use the follow formula: area = base * height * .5

If the user enters 4, the program should end.

Input Validation: Display an error message if the user enters a number outside the range of 1 through 4 when selecting an item from the menu. Do not accept negative values for the circle's radius, the rectangle's length or width, or the triangle's base or height.  

You can use either if/then/else or the switch statement to build your menu.

For extra credit on this assignment, make each of the 3 calculations function calls. This is not required to get full points on the assignment, only if you would like extra credit and extra practice writing modular programs with functions.

Solutions

Expert Solution

PROGRAM:

Please find below the detailed commented program wit different modules to calculate the areas as per user choice:

#include <iostream>//to use cin and cout

#include <iomanip> //to format the output with 2 decimals

using namespace std;

//function prototype

float calculateCircle(float radius); //function to get area of circle

float calculateRactangle(float lenth,float width); //function to get area of ractangle

float calculateTriangle(float base,float height); //function to get area of triangle

//function definitions

float calculateCircle(float radius){

    float PI = 3.14159;//set PI = given value

    float area = PI*radius*radius;//calculate area as per given formula

    return area;//return value of area

}

float calculateRactangle(float length,float width){

    float area = length*width;//calculate area as per given formula

    return area;//return value of area

}

float calculateTriangle(float base,float height){

    float area = base*height* 0.5; //calculate area as per given formula

    return area;//return value of area

}

int main()

{

    int choice;

    float height, weight;

    float area;

   

    cout<<"\tGeometry Calculator!\n";

    cout<<"x-----------------------x\n";

    cout<<"1. Calculate the Area of a Circle\n2. Calculate the Area of a Rectangle\n3. Calculate the Area of a Triangle\n4. Quit\n";

   

    cout<<"Enter your choice(1-4): ";

    cin>>choice;

    if(choice<1||choice>4){//if choice is outside 1 to 4

        cout<<"\nInvalid choice. Quitting... ";

        return 0;

    }//end program when the choice was in correct

    if(choice==1){//if choice is 1

        float radius;//need radius for circle

        cout<<"\nEnter the area of circle: ";

        cin>>radius;

        while(radius<=0){

            cout<<"Radius can not be negative, enter Radius again: ";

            cin>>radius;

        }//end while, when input for raidus is valid

        area = calculateCircle(radius);//call calculateCircle by passing raidus which returns area

        cout<<"\nArea of the circle is: "<<fixed<<setprecision(2)<<area;//print area with 2 decimals        

    }//end if for choice 1

   

    else if(choice==2){//if choice is 2, got for Rectangle

        float length, width;//need length and width for ractangle

        cout<<"\nEnter the length of Rectangle: ";

        cin>>length;

        while(length<=0){

            cout<<"Length can not be negative, enter length again: ";

            cin>>length;

        }//end while, when input for length is valid

        cout<<"\nEnter the width of Rectangle: ";

        cin>>width;

        while(width<=0){

            cout<<"Width can not be negative, enter width again: ";

            cin>>width;

        }//end while, when input for width is valid

        area = calculateRactangle(length,width);//call calculateRactangle by passing height and length which returns area

        cout<<"\nArea of the ractangle is: "<<fixed<<setprecision(2)<<area;//print area with 2 decimals        

    }//end if for choice 2

   

    else if(choice==3){//if choice is 3, go for Triangle

        float base, height;//need base and height for Triangle

        cout<<"\nEnter the base of Triangle: ";

        cin>>base;

        while(base<=0){

            cout<<"Base can not be negative, enter base again: ";

            cin>>base;

        }//end while, when input for base is valid

        cout<<"\nEnter the height of Rectangle: ";

        cin>>height;

        while(height<=0){

            cout<<"Height can not be negative, enter height again: ";

            cin>>height;

        }//end while, when input for height is valid

        area = calculateTriangle(base,height);//call calculateRactangle by passing height and base which returns area

        cout<<"\nArea of the triangle is: "<<fixed<<setprecision(2)<<area;//print area with 2 decimals        

    }//end if for choice 3

    else{//user entered 4 to quit

        cout<<"\nThank you for using Calculator program. Quitting!!!";

        return 0;//exit program

    }

    return 0;

}

SCREENSHOT:

OUTPUT:

Run #1: When choice is invalid:

Run #2: Area of circle:

Run #3: Area of rectangle:

Run #4: Area of Triangle:

Run #5: Quit program:


Related Solutions

---------------------------------------------------------------------------------------------------------------- C++ //code a program for the following the given Instruction. Geometry Calculator 1. Calculate the...
---------------------------------------------------------------------------------------------------------------- C++ //code a program for the following the given Instruction. 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. Use 3.14159 for pi. #03   If the user enters 2, the program should ask for the length and width of...
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...
Write a C/C++ program that simulate a menu based binary numbercalculator. This calculate shall have...
Write a C/C++ program that simulate a menu based binary number calculator. This calculate shall have the following three functionalities:Covert a binary string to corresponding positive integersConvert a positive integer to its binary representationAdd two binary numbers, both numbers are represented as a string of 0s and 1sTo reduce student work load, a start file CSCIProjOneHandout.cpp is given. In this file, the structure of the program has been established. The students only need to implement the following three functions:int binary_to_decimal(string...
Write a program in C++ that solves this problem Calculate the area and volume of a...
Write a program in C++ that solves this problem Calculate the area and volume of a sphere problem. Inside a for loop, vary the radius from 10 to 40  with a step or increment of 5 and calculate the area and volume Your radius will be equal to your loop counter. All calculations should have 2 decimal places, but the radius should have zero decimal places and any number of 1,000 or more should have a comma. Print the radius, area,...
Create a menu-based program that will allow the user to calculate the area for a few...
Create a menu-based program that will allow the user to calculate the area for a few different shapes: square, rectangle, parallelogram, and circle. Refer to the Sample Output in this document to see what menu options you should use. Create PI as a global constant variable. Main Function use do-while to repeat program until user chooses option 5 call the displayMenu function get user choice & validate with while loop depending on user’s choice, get the data required to calculate...
Create a menu-based program that will allow the user to calculate the area for a few...
Create a menu-based program that will allow the user to calculate the area for a few different shapes: square, rectangle, parallelogram, and circle. Refer to the Sample Output in this document to see what menu options you should use. Create PI as a global constant variable. Main Function use do-while to repeat program until user chooses option 5 call the displayMenu function get user choice & validate with while loop depending on user’s choice, get the data required to calculate...
Write a C++ program for the following problem: Calculate and print the area and volume of...
Write a C++ program for the following problem: Calculate and print the area and volume of a cone inside a While  loop that goes from 1 to 20 with a step of .5. (the step is 1/2 or Point 5, so you go 10, 10.5,11, 11.5) Note: Your loop variable will need to be a double data type Use two decimal places on all numbers that are double data type. This will be a table with 3 columns. Don't worry about...
Write a calculator program that prompts the user with the following menu: Add Subtract Multiply Divide...
Write a calculator program that prompts the user with the following menu: Add Subtract Multiply Divide Power Root Modulus Upon receiving the user's selection, prompt the user for two numeric values and print the corresponding solution based on the user's menu selection. Ask the user if they would like to use the calculator again. If yes, display the calculator menu. otherwise exit the program. EXAMPLE PROGRAM EXECUTION: Add Subtract Multiply Divide Power Root Modulus Please enter the number of the...
Write a C++ program to calculate the sphere volume and surface area. Provided that the sphere...
Write a C++ program to calculate the sphere volume and surface area. Provided that the sphere volume = and the sphere surface area = , where r is the radius. 1. Read r from the user using cin statement. 2. Calculate the volume (Vol) and the surface area (Area) of a sphere. 3. Print r, Vol, and Area in a suitable messages.
C++ Write a menu based program for the pet rescue. There should be 2 menu options...
C++ Write a menu based program for the pet rescue. There should be 2 menu options -Add a pet -View pets -If the user chooses option 1, you will open a data file for writing without erasing the current contents, and write a new pet record to it. The file can be formatted any way that you choose but should include the pet's name, species, breed, and color. You my also include any additional information you think is appropriate. -If...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT