//Maxwell Brewer CS1A Chapter 4, P. 225, #21
//
/***************************************************************
*
* CALCULATE AREA OF CIRCLE, RECTANGLE, TRIANGLE
* ________________________________________________
*
* This program will take user inout of a selection
* to calculate either the area of a circle, rectandgle,
* or triangle, then the user will input the measurements.
* _____________________________________________
* INPUT
*
* user choice
* length, width
* base, height
* length, height
*
* OUTPUT
*
* User menu
* Circle sqare units
* Rectangle sqare units
* Triangle sqare units
*
***************************************************************/
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
using namespace std;
int main() {
// Initialization
const char MSG_1[] = "Geometry Calculator";
const char SEL_1[] = " 1. Calculate the Area of a Circle";
const char SEL_2[] = " 2. Calculate the Area of a Rectangle";
const char SEL_3[] = " 3. Calculate the Area of a Triangle";
const char SEL_4[] = " 4. Quit";
const char ENT_1[] = " Enter your choice (1-4):";
int choice; //declare a variable to store user choice.
//Other init. will come later
//Menu
cout << MSG_1 << endl;
cout << SEL_1 << endl;
cout << SEL_2 << endl;
cout << SEL_3 << endl;
cout << SEL_4 << endl
<< endl;
cout << ENT_1 << endl;
// Display input prompts
cin >> choice;
bool valid; //declare a boolean variable to validate input
cout << setprecision(4) << fixed << showpoint;
// Output
if(choice == 1){
const double PI = 3.14159; //declare and initialize pi
double radius; //declare variable to store radius
cout << "Enter radius: " << endl; //prompt to enter radius
cin >> radius; //read radius from keyboard as user input
valid = (radius > 0); //valid will be true if input is positive number
if(valid){ //execute this only if input is valid
//calculate and print area
cout << "The area of the Circle is: ";
cout << (pow(radius, 2) * PI) << " sq. units." <<endl;
}
else{ //execute this if input is invalid
cout << "Invalid input!" << endl;
}
}
else if(choice == 2){
//declare variables to store length and width
double length;
double width;
//prompt to enter length and width
cout << "Enter length and width: " << endl;
//read them from keyboard
cin >> length >> width;
//boolean variable valid will be true if
//both length and width are positive
valid = (length > 0) && (width > 0);
if(valid){ //execute only if input is valid
//calculate and print area
cout << "The area of the Rectangle is: ";
cout << (length * width) << " sq. units." << endl;
}
else{ //execute this if input is invalid
cout << "Invalid input!" << endl;
}
}
else if(choice == 3){
//declare variables to store base and height
double base;
double height;
//prompt to enter base and height
cout << "Enter base and height: " << endl;
//read them from keyboard
cin >> base >> height;
//boolean variable valid will be true if
//both base and height are positive
valid = (base > 0) && (height > 0);
if(valid){ //execute only if input is valid
//calculate and display area
cout << "The area of the Triangle is: ";
cout << base * height/2 << " sq. units." << endl;
}
else{ //execute this if input is invalid
cout << "Invalid input!" << endl;
}
}
else if(choice >= 5){
cout << "ERROR: Choice out of range!" << endl;
}
return 0;
}