// Elaine Torrez Chapter 4 P. 125, #21
/**************************************************************************
* GEOMETRY CALCULATOR
* ------------------------------------------------------------------------
* Displays a menu to calculate the area of a circle, rectangle, or triangle,
* or quit the program. Prompts for dimensions as needed, then shows the
* calculated area or an error message for invalid input.
* ------------------------------------------------------------------------
* INPUT
* choice, radius, length, width, base, height
*
* OUTPUT
* Area of the chosen shape, quit message, or error message
**************************************************************************/
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
const double PI = 3.14159; // Constant for circle area formula
int choice; // User's menu choice
double radius; // Radius of circle
double length, width; // Length and width of rectangle
double base, height; // Base and height of triangle
double area; // Calculated area
// Display menu
cout << "Geometry Calculator\n"
<< "1. Calculate the Area of a Circle\n"
<< "2. Calculate the Area of a Rectangle\n"
<< "3. Calculate the Area of a Triangle\n"
<< "4. Quit\n\n"
<< "Enter your choice (1-4): ";
cin >> choice;
cout << fixed << setprecision(2);
// Process menu choice
switch (choice)
{
case 1: // Circle
cout << "Enter the radius of the circle: ";
cin >> radius;
if (radius < 0)
cout << "Error: Radius cannot be negative.\n";
else
{
area = PI * radius * radius;
cout << "The area of the circle is " << area << endl;
}
break;
case 2: // 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.\n";
else
{
area = length * width;
cout << "The area of the rectangle is " << area << endl;
}
break;
case 3: // Triangle
cout << "Enter the base of the triangle: ";
cin >> base;
cout << "Enter the height of the triangle: ";
cin >> height;
if (base < 0 || height < 0)
cout << "Error: Base and height cannot be negative.\n";
else
{
area = 0.5 * base * height;
cout << "The area of the triangle is " << area << endl;
}
break;
case 4: // Quit
cout << "Program ending.\n";
break;
default:
cout << "Error: Please enter a number between 1 and 4.\n";
}
return 0;
}