//George Molinero csc5 chapter 4, P. 225, #21
//
/*******************************************************************************
*
* Compute Area of Selected Shape
* _____________________________________________________________________________
* This program computes the area of the shape that is selected from the given
* menu.
* _____________________________________________________________________________
* INPUT
* choice : Option selection
* r : radius of cirle
* length : length of rectangle
* width : width of rectangle
* base : base of triangle
* height : height of triangle
*
* OUTPUT
* calculated Area
******************************************************************************/
#include <iostream>
using namespace std;
int main ()
{
const int Circle = 1;
const int Rectangle = 2;
const int Triangle = 3;
const int Quit = 4;
int choice;
float r;
float length;
float width;
float base;
float height;
//
// Display Menu Selection
cout << "Geometry Calculator" << endl << 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 << endl;
//
// Ask for selection
cout << "Enter your choice (1-4):" << endl;
cin >> choice;
//
// input Validation
if (choice >= 1 && choice <= 4)
{
switch(choice)
{
case Circle:
cout << "Enter the radius of the circle: " << endl;
cin >> r;
cout << endl;
if(r >= 0)
cout << "The area of the circle is " << 3.14159 * (r * r) << endl;
else
cout << "radius cannot be negative";
break;
case Rectangle:
cout << "Enter the length of the rectangle: " << endl;
cin >> length;
cout << "Enter the width of the rectangle: " << endl;
cin >> width;
cout << endl;
if(length >= 0 && width >= 0)
cout << "The area of the rectangle is " << length * width << endl;
else
cout << "Both length and width have to be positive";
break;
case Triangle:
cout << "Enter the length of the triagle's base: " << endl;
cin >> base;
cout << "Enter the height of the triangle: " << endl;
cin >> height;
cout << endl;
if(base >= 0 && height >= 0)
cout << "The area of the triangle is " << base * height * 0.5;
else
cout << "Both base and height have to be positive";
break;
case Quit:
cout << endl << "Program ending." << endl;
break;
}
}
else
cout << endl << "ERROR: The valid choices are 1 through 4." << endl;
return 0;
}