//Charlotte Davies-Kiernan CS1A Chapter 4 P.225 #21
//
/******************************************************************************
*
* Display a Geometry Calculator
* ____________________________________________________________________________
* This program will display a men with 4 options: calculate one of three
* area calculations, circle, rectangle, and triangle or to quit.
*
* The formuals that will be used:
* area of circle = PI * radius * radius
* area of rectangle = length * width
* area of triangle = 0.5 * base * height
* ____________________________________________________________________________
* INPUT
* choice //choice the user picks which decides what the program will output
* radius //radius of circle the user will enter if they pick 1
* length //length of rectangle the user will enter if they pick 2
* width //width of rectangle the user will enter if they pick 2
* base //base of triangle the user will enter if they pick 3
* height //height of triangle the user will enter if they pick 3
*
* OUTPUT
* circleArea //Area of circle if the user chooses 1
* recArea //Area of rectangle if the user chooses 2
* triArea //Area of triangle if the user chooses 3
*****************************************************************************/
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
//Variables
double base; //INPUT - base that will need to be entered if user chooses 3
double circleArea; //OUTPUT - area of circle if user chooses 1
double height; //INPUT - height that will need to be entered if user chooses 3
double length; //INPUT - length that will need to be entered if user chooses 2
double radius; //INPUT - radius that will need to be entered if user chooses 1
double recArea; //OUTPUT - area of rectangle if user chooses 2
double triArea; //OUTPUT - area of triangle if user chooses 3
double width; //INPUT - width that will need to be entered if user chooses 2
int choice; //INPUT - the users choice of what the program will output
//
//Display the 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. Calcualte the Area of a Triangle" << endl;
cout << "4. Quit" << endl;
cout << "Enter your choice (1-4): "<< endl << endl;
cin >> choice;
//
//Validate input
if (choice < 1 || choice > 4){
cout << "Error: Invalid choice. Please enter a number from 1 to 4" << endl;
return 1;
}
cout << fixed << setprecision(2);
switch (choice) {
case 1: //Circle
cout << "Enter the radius of the circle: ";
cin >> radius;
if (radius < 0)
cout << "Error: Radius cannot be negative" << endl;
else {
circleArea = M_PI * radius * radius;
cout << "The area of the circle is: " << circleArea << endl;
}
break;
case 2: //Rectangle
cout << "Enter the length of the rectangle: " << endl;
cin >> length;
cout << "Enter the width of the rectangle: " << endl;
cin >> width;
if (length < 0 || width < 0)
cout << "Error: length and width cannot be negative." << endl;
else {
recArea = length * width;
cout << "The area of the rectangle is: " << recArea << endl;
}
break;
case 3: //Triangle
cout << "Enter the base of the triangle: " << endl;
cin>> base;
cout << "Enter the height of the triangle: " << endl;
cin >> height;
if (base < 0 || height < 0)
cout << "Error: Base and height cannot be negative." << endl;
else {
triArea = 0.5 * base * height;
cout << "The area of the triangle is: " << triArea << endl;
}
break;
case 4: //Quit
cout << "Exiting the program. Goodbye!" << endl;
break;
}
return 0;
}