Tuesday, November 22, 2016

Programming Challenge 4.17 - Personal Best

/* Personal Best - This program asks for the name of a pole vaulter and the dates
   and vault heights (in meters) of the athlete's three best vaults. It reports,
   in order of height (best first), the date on which each vault was made and its
   height.
  
   Input Validation: Only values between 2.0 and 5.0 are accepted. */

#include "Utility.h"

int main()
{
    /* Constants for minimum and maximum acceptable heights */
    const double MIN_HEIGHT = 2.0;
    const double MAX_HEIGHT = 5.0;

    /* Hold athlete's name */
    string atheleteName = " ";

    /* Hold heights */
    double heightOne = 0,
           heightTwo = 0,
           heightThree = 0;

    /* Hold dates */
    string dateOne = " ",
           dateTwo = " ",
           dateThree = " ";

    /* Ask the athelete for his name and three heights */
    cout << "Enter your name: ";
    cin >> atheleteName;

    cout << "Enter height One (in meters): ";
    cin >> heightOne;
    cout << "Enter date this height was achieved: ";
    cin >> dateOne;

    cout << "Enter height two (in meters): ";
    cin >> heightTwo;
    cout << "Enter date this height was achieved: ";
    cin >> dateTwo;

    cout << "Enter height three (in meters): ";
    cin >> heightThree;
    cout << "Enter date this height was achieved: ";
    cin >> dateThree;

    /* Format output */
    cout << fixed << showpoint << setprecision(1);

    /* Determine the greatest height and when it was achieved, and display
       the result */
    if (heightOne >= MIN_HEIGHT && heightOne <= MAX_HEIGHT ||
        heightTwo >= MIN_HEIGHT && heightTwo <= MAX_HEIGHT ||
        heightThree >= MIN_HEIGHT && heightThree <= MAX_HEIGHT)
    {
        if (heightOne > heightTwo && heightOne > heightThree)
        {
            cout << "\nYour best result was achieved on: \t" << dateOne
                << "\nThe height achieved was: \t\t" << heightOne << " meters.\n"
                << "\nYour second best result was achieved on:" << dateTwo
                << "\nThe height achieved was: \t\t" << heightTwo << " meters.\n"
                << "\nYour third best result was achieved on:\t" << dateThree
                << "\nThe height achieved was: \t\t" << heightThree << " meters.\n";
        }
        else if (heightOne > heightTwo && heightThree > heightTwo)
        {
            cout << "\nYour best result was achieved on: \t\t" << dateOne
                << "\nThe height achieved was: \t\t" << heightOne << " meters.\n"
                << "\nYour second best result was achieved on:" << dateThree
                << "\nThe height achieved was: \t\t" << heightThree << " meters.\n"
                << "\nYour third best result was achieved on:\t" << dateTwo
                << "\nThe height achieved was: \t\t" << heightTwo << " meters.\n";
        }
        else if (heightTwo > heightOne && heightOne > heightThree)
        {
            cout << "\nYour best result was achieved on: \t" << dateTwo
                << "\nThe height achieved was: \t\t" << heightTwo << " meters.\n"
                << "\nYour second best result was achieved on:" << dateOne
                << "\nThe height achieved was:\t" << heightOne << " meters.\n"
                << "\nYour third best result was achieved on:\t" << dateThree
                << "\nThe height achieved was: \t\t" << heightThree << " meters.\n";
        }
        else if (heightTwo > heightOne && heightThree > heightOne)
        {
            cout << "\nYour best result was achieved on: \t" << dateTwo
                << "\nThe height achieved was: \t\t" << heightTwo << " meters.\n"
                << "\nYour second best result was achieved on:" << dateThree
                << "\nThe height achieved was: \t\t" << heightThree << " meters.\n"
                << "\nYour third best result was achieved on:\t" << dateOne
                << "\nThe height achieved was: \t\t" << heightOne << " meters.\n";
        }
        else if (heightThree > heightTwo && heightOne > heightTwo)
        {
            cout << "\nYour best result was achieved on: \t" << dateThree
                << "\nThe height achieved was: \t\t" << heightThree << " meters.\n"
                << "\nYour second best result was achieved on:" << dateOne
                << "\nThe height achieved was: \t\t" << heightOne << " meters.\n"
                << "\nYour third best result was achieved on:\t" << dateTwo
                << "\nThe height achieved was: \t\t" << heightTwo << " meters.\n";
        }
        else if (heightThree > heightTwo && heightTwo > heightOne)
        {
            cout << "\nYour best result was achieved on: \t" << dateThree
                << "\nThe height achieved was: \t\t" << heightThree << " meters.\n"
                << "\nYour second best result was achieved on:" << dateTwo
                << "\nThe height achieved was: \t\t" << heightTwo << " meters.\n"
                << "\nYour third best result was achieved on:\t" << dateOne
                << "\nThe height achieved was: \t\t" << heightOne << " meters.\n";
        }
        else
        {
            cout << "\nThe input for height is invalid! 2.0 and 5.0 are the lower\n"
                 << "and upper limits.\n";
        }
    }

    pauseSystem();
    return 0;
}

No comments:

Post a Comment