//Jacob Silvestre CSC5 Chapter 4, P. 225, #21
//
/************************************************************************************
*
* GEOMETRY CALCULATOR
*____________________________________________________________________________________
* This program displays a geometry calculator menu and allows the user to calculate
* the area of a circle, rectangle, or triangle.
*
* Circle Area Formula:
* Area = 3.14159 x Radius x Radius
*
* Rectangle Area Formula:
* Area = Length x Width
*
* Triangle Area Formula:
* Area = Base x Height x 0.5
* ____________________________________________________________________________________
* INPUT
* choice : Menu choice chosen by user
* radius : Radius of circle
* length : Length of rectangle
* width : Width of rectangle
* base : Base of triangle
* height : height of triangle
*
* OUTPUT
* area : Calculated area of selected shape
*
**************************************************************************************/
#include <iostream>
using namespace std;
int main() {
const double PI = 3.14159; // Constant value of pi
int choice; // INPUT - Menu selection
double radius; // INPUT - Radius of circle
double length; // INPUT - Length of rectangle
double width; // INPUT - Width of rectangle
double base; // INPUT - Base of triangle
double height; // INPUT - Height of triangle
double area; // OUTPUT - Area of chosen shape
//
// Display Geometry calculator Menu
//
cout << "Geometry Calculator" << endl;
cout << "1. Calculate the Area of a Circle" << endl;
cout << "2. Calculate the Area of a Rectangle" << endl;
cout << "3. Calculate the Area of a Triangle" << endl;
cout << "4. Quit" << endl;
cout << "Enter your choice (1-4): ";
cin >> choice;
//
// Process User Selection
//
switch(choice)
{
case 1:
//
// Input Circle Radius
//
cout << "Enter the radius of the circle: ";
cin >> radius;
//
// Validate Radius
//
if (radius < 0)
{
cout << "ERROR: Radius cannot be negative." << endl;
}
else
{
//
// Compute Circle Area
//
area = PI * radius * radius;
//
// Output Result
//
cout << "The area of the circle is "
<< area << endl;
}
break;
case 2:
//
// Input Rectangle Dimensions
//
cout << "Enter the length of the rectangle: ";
cin >> length;
cout << "Enter the width of the rectangle: ";
cin >> width;
//
// Validate Rectangle Dimensions
//
if (length < 0 || width < 0)
{
cout << "ERROR: Length and width cannot be negative."
<< endl;
}
else
{
//
// Compute Rectangle Area
//
area = length * width;
//
// Output Result
//
cout << "The area of the rectangle is "
<< area << endl;
}
break;
case 3:
//
// Input Triangle Dimensions
//
cout << "Enter the base of the triangle: ";
cin >> base;
cout << "Enter the height of the triangle: ";
cin >> height;
//
// Validate Triangle Dimensions
//
if (base < 0 || height < 0)
{
cout << "ERROR: Base and height cannot be negative."
<< endl;
}
else
{
//
// Compute Triangle Area
//
area = base * height * 0.5;
//
// Output Result
//
cout << "The area of the triangle is "
<< area << endl;
}
break;
case 4:
//
// End Program
//
cout << "Program ending." << endl;
break;
default:
//
// Output Error Message
//
cout << "ERROR: Enter a number from 1 through 4." << endl;
}
return 0;
}