Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using C++ on Virtual Studio 2017 write a program that displays the following men

ID: 3726480 • Letter: U

Question

Using C++ on Virtual Studio 2017 write a program that displays the following 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 and display its area. Use the following formula: area = pi times radius squared. Use 3.14159 for pi.

If the user enters 2, the program should ask for the length and width of the rectangle and display the area. Use the formula area = length times width.

If the user enters 3, the program should ask for the length of the triangle’s base and its height. Display the area. Use the formula area = base times height times 0.5.

If the user enters 4 the program should end.

selection of choices 1 through 4 must use a switch case.

Input Validation: Display an error message if the user enters a number outside the range of 1 through 4. Do not accept negative values for any of the inputs.

Please use doubles as input for the measurements

The code must allow for looping for selections, wrong selections and validating input.

Your selection of choices 1 through 4 must use a switch case.

Must not allow values below zero for circle, rectangle or triangle inputs.

MUST USE THE FOLLOWING PROTOTYPES IN THE CODE:

//Prototypes

int displayMenu(); //displays menu and returns validated selection

double calcAreaCircle( double radius ); //returns the area of the circle, validate input

double calcAreaRect( double length, double width ); //returns the area of a rectangle, validate input

double calcAreaTriangle( double base, double height ); //returns the area of a triangle, validate input

Explanation / Answer

C++ program to find area of circle , rectangle, triangle with functions

#include <cstdlib>
#include <iostream>
#include<iomanip>

using namespace std;
double pi = 3.14159;
//Prototypes

int display_menu(); //displays menu and returns validated selection

double calcAreaCircle( double radius ); //returns the area of the circle, validate input

double calcAreaRect( double length, double width ); //returns the area of a rectangle, validate input

double calcAreaTriangle( double base, double height ); //returns the area of a triangle, validate input

int main() {

int choice; //for accepting user choice
  
double radius, area, length, breath, base, height;

//do loop is used to perform the operation until the user exit
do {
choice = display_menu(); //displays the menu

//switch case is used to select the option entered by the user
switch (choice) {
case 1://if choice is 1
//prompt the user for radius
std::cout << "Enter the radius of circle : ";
cin >> radius;
area = calcAreaCircle(radius);
std::cout << " Area of circle is : " << area<<std::endl;
break;
case 2://if the option is 2
//prompt the user to enter the length and breath
std::cout << " Enter Length of the Rectangle : ";
cin>>length;
std::cout << " Enter width of the rectangle :";
cin>>breath;
area =calcAreaRect(length,breath);
//display the area
std::cout << " Area of Rectangle: "<< area<<std::endl;
break;
case 3://if the option 3
//prompt the user for base and height
std::cout << " Enter the base of the triangle: ";
cin >> base;
std::cout << " Enter the height of the triangle : ";
cin >> height;
area = calcAreaTriangle(base, height );
//display the result
std::cout << " Area of triangle is :" << area <<std::endl;
break;
case 4://exit
std::cout << "Thank you.!!!";
break;
default://if the user enters other values
std::cout << "please choose the correct option(1-4). ";

}
}
while(choice!=4);
  
return 0;
}

int display_menu()
{//function for displaying the menu
int choice;
  
std::cout << "-----------------------------------------------" << std::endl;
std::cout << " Geometry Calculator" << std::endl;
std::cout << "------------------------------------------------" << std::endl;
std::cout << " 1.Calculate the are of a circle" << std::endl;
std::cout << " 2. Calculate the area of a Rectangle" << std::endl;
std::cout << " 3. Calculate the area of a Triangle" << std::endl;
std::cout << " 4. Quit." << std::endl;
  
//prompt for user input of choice
do{
std::cout << "Please Enter the digit corresponding to your case :";
cin>>choice;
  
if((choice<1)||(choice>4))
std::cout<<"Sorry!!!invalid entry...Enter (1-4) :"<<std::endl;
} while ((choice<1)||(choice>4));
return(choice);
}

//function to validate the input and calculate the area
double calcAreaCircle( double radius ){
while(radius<1){
std::cout<<" Please enter the valid positive numbers"<<std::endl;
std::cout << "Enter the radius of circle : ";
cin >> radius;
}
  
return(pi * radius * radius);
}

//function to validate the input and calculate the area
double calcAreaRect( double length, double width ){
while(length<1){
std::cout<<" Please enter the valid positive numbers"<<std::endl;
std::cout << "Enter the length of the rectangle : ";
cin >> length;
}
while(width<1){
std::cout<<" Please enter the valid positive numbers"<<std::endl;
std::cout << "Enter the width of the rectangle : ";
cin >> width;
}
return(length*width);  
  
}
//function to validate the input and calculate the area
double calcAreaTriangle( double base, double height ){
while(base<1){
std::cout<<" Please enter the valid positive numbers"<<std::endl;
std::cout << "Enter the base : ";
cin >> base;
}
while(height<1){
std::cout<<" Please enter the valid positive numbers"<<std::endl;
std::cout << "Enter the height : ";
cin >> height;
}
return(base*height*0.5);  
}

Sample Run:

-----------------------------------------------
Geometry Calculator
------------------------------------------------
1.Calculate the are of a circle
2. Calculate the area of a Rectangle
3. Calculate the area of a Triangle
4. Quit.
Please Enter the digit corresponding to your case :9
Sorry!!!invalid entry...Enter (1-4) :
Please Enter the digit corresponding to your case :1
Enter the radius of circle : -7

Please enter the valid positive numbers
Enter the radius of circle : 0

Please enter the valid positive numbers
Enter the radius of circle : 10

Area of circle is : 314.159
-----------------------------------------------
Geometry Calculator
------------------------------------------------
1.Calculate the are of a circle
2. Calculate the area of a Rectangle
3. Calculate the area of a Triangle
4. Quit.
Please Enter the digit corresponding to your case :4
Thank you.!!!
RUN SUCCESSFUL (total time: 29s)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote