Friday, November 25, 2016

Programming Challenge 4.23 - Geometry Calculator

/* Geometry Calculator - This program displays the following menu:

   Geometry Calculator

        1. Calculate the Area of a Circle
        2. Calculate the Area of a Rectangle
        3. Calculate the Area of a Triangle
        4. Quit

   Enter your choice (1-4):

   If the user enters 1, the program asks for the radius of the circle and then
   displays its area. The following formula applies:
   * area = πr2
   * for π = 3.14159 is used and the radius of the circle for r.

   If the user enters 2, the program asks for the length and width of the rectangle
   and then displays the rectangle's area. The following formula applies:
   * area = length * width

   If the user enters 3, the program asks for the length of the triangle's base and
   its height, and then displays its area. The following formula applies:
   * area = base * height * .5

   If the user enters 4, the program will end.

   Input Validation: The program will display an error message if the user enters a
   number outside the range of 1 through 4 when selecting an item from the menu.
   Negative values are not accepted for the circle's radius, the rectangle's length
   or width, or the triangle's base or height. */

#include "Utility.h"

int main()
{
    /* Hold constants for menu choice */
    const int CIRCLE_AREA = 1,
              RECTANGLE_AREA = 2,
              TRIANGLE_AREA = 3,
              QUIT = 4;

    /* Hold constant for PI */
    const double PI = 3.14159;

    /* Hold menu choice */
    int choice = 0;

    /* Hold area, radius for circle, length and width for rectangle, base and height
       for triangle */
    double area,
           radiusCircle = 0,
           lengthRect = 0,
           widthRect = 0,
           baseTri = 0,
           heightTri = 0;

    /* Display the menu and ask for a choice */
    cout << "Geometry Calculator\n\n"
         << "\t1. Calculate the Area of a Circle\n"
         << "\t2. Calculate the Area of a Rectangle\n"
         << "\t3. Calculate the Area of a Triangle\n"
         << "\t4. Quit\n\n"
         << "\tEnter your choice (1-4): ";
    cin >> choice;

    /* Formating the output to a precision of 2 places behind the comma to get a
       round(er) number */
    cout << fixed << showpoint << setprecision(2);

    /* Switch-case statements */
    switch (choice)
    {
    case 1:
        /* Ask for the radius of the circle */
        cout << "\nPlease enter the radius of the circle: ";
        cin >> radiusCircle;

        /* Calculation */
        area = PI * pow(radiusCircle, 2);

        /* Validating the input and displaying the result */
        if (radiusCircle <= 0)
        {
            cout << "\nPlease enter a positive value, above 0, for the circle's "
                 << "radius.\n";
        }
        else
        {
            cout << "\nA circle with a radius of " << radiusCircle << " cm "
                 << " has an area of: " << area << " square cm\n";
        }
        break;

    case 2:
        /* Ask for the length and width of the rectangle */
        cout << "\nPlease enter the length of the rectangle: ";
        cin >> lengthRect;
        cout << "Now enter the width of the rectangle: ";
        cin >> widthRect;

        /* Calculations */
        area = lengthRect * widthRect;

        /* Validating the input and displaying the result */
        if (lengthRect <= 0 || widthRect <= 0)
        {
            cout << "\nPlease enter a positive value, above 0, for the rectangle's "
                << "length and width.\n";
        }
        else
        {
            cout << "\nA rectangle with a length of " << lengthRect << " cm"
                 << " and a width of " << widthRect << " cm "
                 << " has an area of " << area << " sq cm\n";
        }
        break;

    case 3:
        /* Ask the user for base and height of the triangle */
        cout << "\nPlease enter the base of the triangle: ";
        cin >> baseTri;
        cout << "Now enter the height of the triangle: ";
        cin >> heightTri;

        /* Calculations */
        area = (baseTri * heightTri) * .5;

        /* Validating the input and displaying the result */
        if (baseTri <= 0 || heightTri <= 0)
        {
            cout << "\nPlease enter a positive value, above 0, for the triangle's "
                 << "base and height.\n";
        }
        else
        {
            cout << "\nA triangle with a base of " << baseTri << " cm"
                 << " and a height of " << heightTri << " cm"
                 << " has an area of " << area << " sq cm\n";
        }
        break;

    case 4:
        cout << "\nYou chose to quit for this time. Thank's for using this program!\n";
        break;

    default:
        cout << "\nYou entered an invalid menu choice. Please choose between 1 and 3\n"
             << "or 4 to quit this program.\n";
    }

    pauseSystem();
    return 0;
}

No comments:

Post a Comment