//Amari Mosley CSC5 Chapter 4, P.224 #21
//
/**************************************************************
*
* Geometry Calculator
* ____________________________________________________________
* This program displays calculates the area of a circle,
* rectangle, or triangle, or quit.
*
* Computation is based on standard geometric formulas:
* Circle Area = 3.14159 * r^2
* Rectangle Area = length * width
* Triangle Area = base * height * 0.5
* ____________________________________________________________
* INPUT
* choice : Menu selection (1-4)
* radius : Radius of the circle
* length : Length of the rectangle
* width : Width of the rectangle
* base : Base of the triangle
* height : Height of the triangle
*
* OUTPUT
* area : Calculated geometric area
*
**************************************************************/
#include <iostream>
#include <iomanip>
using namespace std;
// Defining Main Function
int main ( )
{
// Defining Constants and Variables
const double PI = 3.14159;
int choice;
double radius;
double length;
double width;
double base;
double height;
double area;
// Displaying Menu
cout << "Geometry Calculator\n";
cout << "1. Calculate the Area of a Circle\n";
cout << "2. Calculate the Area of a Rectangle\n";
cout << "3. Calculate the Area of a Triangle\n";
cout << "4. Quit\n";
cout << "Enter your choice (1-4): ";
cin >> choice;
// Processing User Choice
switch (choice)
{
case 1:
// Area of Circle
cout << "Enter the radius of the circle: ";
cin >> radius;
if (radius < 0)
{
cout << "Error: Radius cannot be negative." << endl;
}
else
{
area = PI * radius * radius;
cout << "The area of the circle is " << area << endl;
}
break;
case 2:
// Area of Rectangle
cout << "Enter the length of the rectangle: ";
cin >> length;
cout << "Enter the width of the rectangle: ";
cin >> width;
if (length < 0 || width < 0)
{
cout << "Error: Length and width cannot be negative." << endl;
}
else
{
area = length * width;
cout << "The area of the rectangle is " << area << endl;
}
break;
case 3:
// Area of Triangle
cout << "Enter the length of the triangle's base: ";
cin >> base;
cout << "Enter the height of the triangle: ";
cin >> height;
if (base < 0 || height < 0)
{
cout << "Error: Base and height cannot be negative." << endl;
}
else
{
area = base * height * 0.5;
cout << "The area of the triangle is " << area << endl;
}
break;
case 4:
// Quit
cout << "Program ending." << endl;
break;
default:
// Input Validation for Menu Choice
cout << "Error: Valid choices are 1 through 4." << endl;
}
return 0;
}