1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
/// @author Alexander Corey
/// @attention I pledge my word of honor that I have abided by the CSN
/// Academic Integrity Policy while completing this assignment.
/// @file pa06a.cpp
/// @date 2017-10-22
/// @brief This program prompts the user to input three weather-related values
/// and outputs the wind chill factor and heat index.
/// @note I spent about 6 hours developing this program. To improve code
/// readability, I used the digit separator character ' from C++14 to break up
/// numbers in the formulas. In addition to the textbook, I referenced
/// cppreference.com. To help craft a function to restore a failed input
/// stream, I referenced the clear_failure function in Kevin's lab06a solution.

#include <iostream>
#include <cmath>
#include <iomanip>
#include <string>

using namespace std;

// Function prototypes
void userInput(double& degrees, double& windSpeed, double& humidity);
double windChill(double degrees, double windSpeed);
double heatIndex(double degrees, double humidity);
void inputFailure(istream& cin);

// Const variables
// OPENING_PROMPT holds string for prompt for user input
const string OPENING_PROMPT = "Please enter the temperature in degrees "
                              "Fahrenheit, the wind speed in miles per "
                              "hour and the humidity as a "
                              "percentage between 0 and 100.";
// RETRY_HUMIDITY holds string for prompt for retry of user humidity input
const string RETRY_HUMIDITY = "Please re-enter the humidity as a "
                              "percentage between 0 and 100.";
// WIND_CHILL_IS string is for final wind chill output
const string WIND_CHILL_IS =  "Wind chill factor is: ";
// HEAT_INDEX_IS string is for final heat index output
const string HEAT_INDEX_IS =  "Heat index is: ";

// main function, primary entry point for program
int main()
{
    // Local variables
    double degrees = 0.0;   // holds the value of user input degrees variable
    double windSpeed = 0.0; // holds the value of user input wind mph variable
    double humidity = 0.0;  // holds the value of user input humidity variable

    cout << fixed << setprecision(1) << '\n';

    userInput(degrees, windSpeed, humidity);

    cout << WIND_CHILL_IS << windChill(degrees, windSpeed) << '\n';

    cout << HEAT_INDEX_IS << heatIndex(degrees, humidity) << endl;

    return 0;
}

// userInput function takes input for weather-related variable values
// degrees, windSpeed & humidity are passed by reference so that user
// input values change values of variables of same name declared in main
void userInput(double& degrees, double& windSpeed, double& humidity)
{
    cout << OPENING_PROMPT << '\n';
    cin >> degrees >> windSpeed >> humidity;
    while (!cin) {
        inputFailure(cin);
        userInput(degrees, windSpeed, humidity);
    }
    while (humidity < 0 || humidity > 100) {
        cout << RETRY_HUMIDITY << '\n';
        cin >> humidity;
    }
}

// Function windChill uses degrees, windSpeed variables to calculate and
// return wind chill
// Parameters degrees, windSpeed passed by value from variables of
// same name in main for windChill calculations
double windChill(double degrees, double windSpeed)
{
    double windChill; // holds wind chill calculation to return to main
    windChill = 35.74 + (0.6'215*degrees) - (35.75*pow(windSpeed,0.16)) +
                         (0.4'275*degrees)*pow(windSpeed,0.16);
    return windChill;
}

// Function heatIndex uses degrees, humidity variables to calculate and
// and return heat index
// Parameters degrees, humidity passed by value from variable of same name
// in main for heatIndex calculations
double heatIndex(double degrees, double humidity)
{
    double heatIndex; // holds heatIndex calculation to return to main
    heatIndex = -42.379 + 2.04'901'523*degrees + 10.14'333'127*humidity +
                -0.22'475'541*degrees*humidity +
                -6.83'783*pow(10,-3)*pow(degrees,2) +
                -5.481'717*pow(10,-2)*pow(humidity,2) +
                1.22'874*pow(10,-3)*pow(degrees,2)*humidity +
                8.5'282*pow(10,-4)*degrees*pow(humidity,2) +
                -1.99 * pow(10, -6)*pow(degrees,2)*pow(humidity,2);
    return heatIndex;
}

// In the event of a bad user input, inputFailure function takes an istream
// variable and restores program to working order.
void inputFailure(istream& cin)
{
    cin.clear();
    cin.ignore(200,'\n');
}