Saturday, June 22, 2013

Geometric Calculator

#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

//declare constants.
const double PI = 3.14159;

int main()

{

int userChoice;
double circleRadius, areaCircle, rectangleLenght, width, rectangleArea, triangleBase, triangleHeight, triangleArea;


 // Display the menu and allow user to choose 1-4.
 cout << "\t\tGeometry Calculator"<<'\n';
 cout << "\t1. Calculate the Area of a Circle"<<'\n';
 cout << "\t2. Calculate the Area of a Rectangle"<<'\n';
 cout << "\t3. Calculate the Area of a Triangle"<<'\n';
 cout << "\t4. Quit\n\n";
 cout << "\tEnter your choice (1-4): ";

 cin >> userChoice;
 //format output precision.
 cout << fixed << showpoint << setprecision(2);

 // Validate menu userChoice.
 if (userChoice < 1 || userChoice > 4)
 {
 cout << endl;
 cout << "\t\t**Error!!!"<<'\n';
 cout<< "\tMenu option must be 1, 2, 3, or 4.\n\n";
 }

 // Respond to user's menu userChoice.
 switch (userChoice)
 {
 case 1: // Find area of a circle
 cout << endl;
 cout << "\t\tArea of a Circle\n\n";
 cout << "\t Please enter the radius of the circle: ";
 cin >> circleRadius;
 if (circleRadius < 0)
 {
 cout << '\n'<<endl;
 cout << "\t\t!!! Invalid Input Data"<<'\n'<<endl;
 cout<<"The radius of the circle must be a positive number and different from 0"<<'\n'<<endl;
 }
 else
 {
 areaCircle = PI * pow(circleRadius, 2.0);
 cout << "\tArea = " << areaCircle << endl;
 }
 break;

 case 2: // Find area of a rectangle
 //format output precision.
 cout << fixed << showpoint << setprecision(2);
 cout << endl;
 cout << "\t\tArea of a Rectangle\n\n";
 cout << "\tPlease enter the length of the rectangle: ";
 cin >> rectangleLenght;
 cout << "\tPlease enter the width of the rectangle: ";
 cin >> width;
 if (rectangleLenght <= 0 || width <= 0)
 {
 cout << '\n'<<endl;
 cout << "\t\t!!! Invalid Input Data"<<'\n'<<endl;
 cout<<"The Length and Width must be a positive number and different from 0"<<'\n'<<endl;
 }
 else
 {
 rectangleArea = rectangleLenght * width;
 cout << "\tArea = " << rectangleArea << " square units"<<endl;
 }
 break;

 case 3: // Find area of a triangle
 //format output precision.
 cout << fixed << showpoint << setprecision(2);
 cout << endl;
 cout << "\t\tArea of a Triangle\n\n";
 cout << "\tPlease enter the base of the triangle: ";
 cin >> triangleBase;
 cout << "\tPlease enter the height of the triangle: ";
 cin >> triangleHeight;
 if (triangleBase <= 0 || triangleHeight <= 0)
 {
 cout << '\n'<<endl;
 cout << "\t\t!!! Invalid Input Data"<<'\n'<<endl;
 cout<<"The Base and Height must be a positive number and different from 0"<<'\n'<<endl;

 }
 else
 {
 triangleArea = triangleBase * triangleHeight * 0.5;
 cout << "\tArea = " << triangleArea << endl;
 }
 break;

 case 4: // Quit the program
 cout << "\n\n\tThank you for using this software. ";
 cout<<endl;
 }

 system("pause");
 return 0;
 }


No comments:

Post a Comment