#include <iostream>
#include <iomanip>
constexprauto pi = 3.1415;
usingnamespace std;
// Function prototypes
char getShapeType();
double getAreaOfRectangle(double, double);
double getAreaOfCircle(double);
double getAreaOfTriangle(double, double);
double getAreaOfSquare(double);
int main()
{
char shapeType; // R=rectangle, T=triangle, C=circle, S= square
double areaOfRectangle; //variable to store the area of rectangle
double areaOfCircle; //variable to store the area of circle
double areaOfTriangle; //variable to store the area of triangle
double areaOfSquare; //variable to store the area of circle
// Get the shape type.
shapeType = getShapeType();
if (shapeType == 'R')
{
double width;
double length;
do
{
cout << "Enter width and length of the rectangle separated by space: ";
cin >> width >> length;
} while (width <= 0 || length <= 0);
areaOfRectangle = getAreaOfRectangle(width, length);
cout << "The area of the rectangle with width "
<< width << " and length " << length
<< " is " << areaOfRectangle << endl;
}
if (shapeType == 'C')
{
double radius;
do
{
cout << "Enter radius of the circle: ";
cin >> radius;
} while (radius <= 0);
areaOfCircle = getAreaOfCircle(radius);
cout << "The area of the circle with radius "
<< radius << 'is' << areaOfCircle << endl;
}
if (shapeType == 'T')
{
double base;
double height;
do
{
cout << "Enter base and height of the triangle separated by space: ";
cin >> base >> height;
} while (base <= 0 || height <= 0);
areaOfTriangle = getAreaOfTriangle(base, height);
cout << "The area of the triangle with base "
<< base << " and height " << height
<< " is " << areaOfTriangle << endl;
}
if (shapeType == 'S')
{
double width;
do
{
cout << "Enter width of the square: ";
cin >> width;
} while (width <= 0);
areaOfSquare = getAreaOfSquare(width);
cout << "The area of the square with width "
<< width << 'is' << areaOfSquare << endl;
}
system("pause");
return 0;
}
//*********************************************************
// The getShapeType function returns 'C' or 'R' or 'S' or 'T' to *
// indicate circle or rectangle or square or triangle. *
//*********************************************************
char getShapeType()
{
char type; // The shape type
// Get the shape type, R, C, T, S
cout << "This program will compute area of a shape.\n"
<< "What is the shape type?\n"
<< "Circle or Rectangle or Square or Triangle? (C or R or S or T): ";
cin >> type;
// Validate the shape type.
while (type != 'C' && type != 'c' &&
type != 'R' && type != 'r' && type != 'S' && type != 's'
&& type != 'T' && type != 't')
{
cout << "Please enter C or R or S or T: ";
cin >> type;
}
// Convert lowercase to uppercase.
if (type == 'c')
type = 'C';
elseif (type == 'r')
type = 'R';
elseif (type == 's')
type = 'S';
elseif (type == 't')
type = 'T';
return type;
}
//*****************************************
//Function to calculate the are of rectangle
// Area = width*length
//******************************************
double getAreaOfRectangle(double width, double length)
{
double area = width * length;
return area;
}
//*****************************************
//Function to calculate the area of circle
// Area = pi * r * r
//******************************************
double getAreaOfCircle(double radius)
{
double area = pi * radius * radius;
return area;
}
//*****************************************
//Function to calculate the area of square
// Area = width*width
//******************************************
double getAreaOfSquare(double width)
{
double area = width * width;
return area;
}
//*****************************************
//Function to calculate the area of triangle
//Area = (base * height)/2
//****************************************
double getAreaOfTriangle(double base, double height)
{
double area = (base * height) / 2;
return area;
}
Thank you and yea I can tell a lot could be shortened. Our professor gave us a template only for the area of a rectangle, so we had to pretty much go off his work.