//Natalie Zarate CIS 5 Chapter 4, P. 222, #21
/*******************************************************************************
*
* COMPUTE SHAPE AREA
* _____________________________________________________________________________
* This program accepts will display a menu of 4 different shape areas the user
* can prompt the program to calculate. The program, will accept numbers greater
* than zero and use the user input to calculate the area of the selected shape,
* using the formulas 2(pi)r^2 , l * w, or (b * h) / 2.
* _____________________________________________________________________________
* INPUT
* numChoice : The choice the user picks from the menu
* radius : Radius of circle
* pi : value of pi
* length : Length of rectangle
* width : Width of rectangle
* base : Base of triangle
* height : Height of triangle
*
* OUTPUT
* area : area of shape
*
******************************************************************************/
#include <iostream>
#include <iomanip>
using namespace std;
int main ()
{
/**********************************************************************
* CONSTANTS
* --------------------------------------------------------------------
* pi - value of pi
* *******************************************************************/
const float pi = 3.14159;
int numChoice; // INPUT - The choice the user picks from the menu
float radius; // INPUT - Radius of circle
float length; // INPUT - Length of rectangle
float width; // INPUT - Width of rectangle
float base; // INPUT - Base of triangle
float height; // INPUT - Height of triangle
float area; // OUTPUT - Area of shape
// Prompt user menu
cout << "Geometry Calculator" << endl;
cout << setw(5) << "1." << " Calculate the Area of a Circle" << endl;
cout << setw(5) << "2." << " Calculate the Area of a Rectangle" << endl;
cout << setw(5) << "3." << " Calculate the Area of a Triangle" << endl;
cout << setw(5) << "4." << " Quit" << endl;
cout << endl;
cout << setw(8) << "Enter" << " your choice (1-4):" << endl;
cin >> numChoice;
// Validate input, calculate, and display
switch (numChoice)
{
case 1: cout << "Enter the circle's radius:" << endl;
cin >> radius;
if (radius < 0)
{
cout << "Radius cannot be negative, re-start program and";
cout << " try again.";
}
else
{
area = 2 * pi * radius;
cout << "The area of the circle is " << area << ". \n";
}
break;
case 2: cout << "Enter the length of the rectangle:" << endl;
cin >> length;
if (length < 0)
{
cout << "Length cannot be negative, re-start program and";
cout << " try again.";
}
cout << "Enter the width of the rectangle:" << endl;
cin >> width;
if (width < 0)
{
cout << "Width cannot be negative, re-start program and";
cout << " try again.";
}
else
{
area = length * width;
cout << "The area of the rectangle is " << area << ". \n";
}
break;
case 3: cout << "Enter the length of the base of the triangle:" << endl;
cin >> base;
if (base < 0)
{
cout << "Base cannot be negative, re-start program and";
cout << " try again.";
}
else
{
cout << "Enter the height of the triangle" << endl;
cin >> height;
}
if (height < 0)
{
cout << "Height cannot be negative, re-start program and";
cout << " try again.";
}
else
{ area = (base * height) / 2;
cout << "The area of the triangle is " << area << ". \n";
}
break;
case 4: cout << "Program ending. \n";
break;
default: cout << "Please choose from options 1-4, re-run the program";
cout << "and try again." << endl;
break;
}
return 0;
}