In: Computer Science
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.
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: