// Cenyao Huang CS1A Homework 4, p. 225, #21
/******************************************************************************************************
* CALCULATE GEOMETRY
*
* This program calculates the area of a circle, rectangle, and triangle.
*
* This program uses the following formulas :
* Area of a circle : 2 * 3.14159 * radius
* Area of a rectangle : length * width
* Area of a triangle : 1/2 * base * height
*
* Input
* radius : the value of the radius of the circle
* length : the value of the length of the rectangle
* width : the value of the width of the rectangle
* base : the value of the base of the triangle
* height : the vallue of the height of the triangle
* area: the value of the area of either the circle, rectangle, or triangle
*******************************************************************************************************/
#include <iostream>
using namespace std;
int main() {
// assign variables
double area;
int num = 0;
//display menu
cout << "Geometry Calculator" << endl << "\t 1. Calculate the Area of a Circle" << endl << "\t 2. Calculate the Area of a Rectangle" << endl << "\t 3. Calculate the Area of a Triangle" << endl << "\t 4. Quit" << endl;
// if the value entered in the menu is not 4, it will loop
while (num != 4)
{
cin >> num;
if (num > 4 || num < 1)
cout << "Number outside of range.";
// get values to calculate area, then display it
switch (num) {
case 1:
double radius;
cout << "Please enter the radius of the circle" << endl;
cin >> radius;
area = 2 * 3.14159 * radius;
cout << "The area of the circle is: " << area << endl;
if (radius < 0)
cout << "Invalid input. Please enter positive numbers.";
break;
case 2:
double length, width;
cout << "Please enter the length and width of the rectangle" << endl;
cin >> length >> width;
area = length * width;
cout << "The area of the rectangle is" << area << endl;
if (length < 0 || width < 0)
cout << "Invalid input. Please enter positive numbers.";
break;
case 3:
double base, height;
cout << "Please enter the base and height for the triangle" << endl;
cin >> base >> height;
area = base * height;
cout << "The area of the base is: " << area << endl;
if (base < 0 || height < 0)
cout << "Invalid input. Please enter positive numbers.";
break;
case 4:
cout << "End of program";
break;
}
}
return 0;
}