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...
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...
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.
Goals Practice conditional statements Description Write a program to simulate a menu driven calculator that performs...
Goals Practice conditional statements Description Write a program to simulate a menu driven calculator that performs basic arithmetic operations (add, subtract, multiply and divide). The calculator accepts two numbers and an operator from user in a given format: For example: Input: 6.3 / 3 Output: 2.1 Create a Calculator class that has a method to choose the right mathematical operation based on the entered operator using switch case. It should then call a corresponding method to perform the operation. In...
1. Specification Write a C program to implement a simple calculator that accepts input in the...
1. Specification Write a C program to implement a simple calculator that accepts input in the following format and displays the result of the computation: calc [operand_1] [operator] [operand_2] The operands operand_1 and operand_2 are non-negative integers. The operator is one of the following: addition (+), subtraction (-), multiplication (x), division (/) and modulo (%). Note: For the multiplication operator, use letter ‘x’. If you use the asterisk ‘*’, your program will not work properly 2. Implementation • The program...
In Python, write a calculator that will give the user the following menu options: 1) Add...
In Python, write a calculator that will give the user the following menu options: 1) Add 2) Subtract 3) Multiply 4) Divide 5) Exit Your program should continue asking until the user chooses 5. If user chooses to exit give a goodbye message. After the user selections options 1 - 4, prompt the user for two numbers. Perform the requested mathematical operation on those two numbers. Round the result to 1 decimal place. Example 1: Let's calculate! 1) Add 2)...
Write a menu-driven C++ program with two options: 1) Is it odd or even? 2) End...
Write a menu-driven C++ program with two options: 1) Is it odd or even? 2) End Program.The user can only input a positive integer (0 does not count) and if the input is not a positive integer the program must state "Invalid Input." The program must determine whether the input is even or odd. The program must use functions and can only use the iostream and iomanip libraries. Note: The program must loop until the 2nd menu option is chosen.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT