summaryrefslogtreecommitdiff
path: root/BlankConsoleLab/CST116 -Lab2-Hill.cpp
blob: 6e96b6da0a822fb1f6a4578c48dea52de9c52a04 (plain) (blame)
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// BlankConsoleLab.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

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

using namespace std;

float tempCelsius, windCelsius;
float tempFahrenheit, windFahrenheit;
float windSpeed;

// Constants for the range of acceptable temperatures and wind speed.

const float minF = -80.0, maxF = 121.0;
const float minC = -62.0, maxC = 49.5;
const float minW = 0, maxW = 231;

float ConvertTemperature(float temperature, char initialSystem) {

    // If initialSystem is 'C', temperature is converted into fahrenheit. If initialSystem is 'F', temperature is converted into celsius.

    float convertedTemp;

    switch(initialSystem) {

    case 'C':
        // Converting celsius to fahrenheit, return the result.

        convertedTemp = (temperature * 1.8) + 32.0;
        return convertedTemp;

    case 'F':
        // Converting fahrenheit to celsius, return the result.

        convertedTemp = (temperature - 32.0) / 1.8;
        return convertedTemp;
    }

}

bool CheckWindSpeed(float& input) {

    // Checks to see if the value entered is between the minimum and maximum wind speeds.

    if (input < minW || input > maxW) {

        cout << "\nInvalid wind speed. Please enter a number between 0 and 231" << endl;
        return false; // Returns false if the value is beyond or below the range.

    }
    else return true;

}

float GetWindChill() {

    // Simply calculates the wind chill in fahrenheit, and converts it to celsius using a previous function

    float windChill;

    windChill = 35.74 + (0.6215 * tempFahrenheit) + (pow(windSpeed, 0.16) * ((0.4275 * tempFahrenheit) - 35.75));
    
    return windChill; // Returns the wind chill based on what the user entered before the function has been called. Only outputs as fahrenheit.

}

int main()
{
    char choice = 'Y';

    while (choice == 'Y') {

        cout << "Please enter a temperature between -80 to 121 degrees fahrenheit, or -62 and 49.5 degrees celsius." << endl;
        cout << "Example entries: -32.3 F, 27.8 C\n" << endl;

        float tempInput;
        char tempSystem;

        bool validEntries = false;

        // Taking input from the user until they give a valid entry.
        // A valid entry should be a number and the character C for celsius or F for fahrenheit.

        do {

            cin >> tempInput >> tempSystem;

            if (tempSystem == 'C') {

                if (tempInput < minC || tempInput > maxC) {

                    cout << "\nTemperature in celsius must be between -62 and 49.5 degrees. Please try again." << endl;

                }
                else {
                    validEntries = true;
                    tempCelsius = tempInput;
                    tempFahrenheit = ConvertTemperature(tempInput, 'C');
                }

            }
            else if (tempSystem == 'F') {

                if (tempInput < minF || tempInput > maxF) {

                    cout << "\nTemperature in fahrenheit must be between -80 and 121 degrees. Please try again." << endl;

                }
                else {
                    validEntries = true;
                    tempFahrenheit = tempInput;
                    tempCelsius = ConvertTemperature(tempInput, 'F');
                }

            }
            else {

                cout << "\nInvalid temperature system. Please try again with C or F as your unit." << endl;

            }

        } while (validEntries == false);

        cout << "\nNext, enter a wind speed between 0 and 231mph. You do not need to include a unit." << endl;

        // validEntries is just a boolean that's getting reused.

        validEntries = false;

        do {

            cin >> windSpeed;

            validEntries = CheckWindSpeed(windSpeed); // If the user's input is between the minium and maximum wind speeds, this will return true.

        } while (validEntries == false);

        // Setting the wind chill values.

        windFahrenheit = GetWindChill();
        windCelsius = ConvertTemperature(windFahrenheit, 'F');

        int colWidth = 20;

        cout << "\nConversions for " << tempInput << tempSystem << " with wind speed " << windSpeed << "MPH." << endl;

        // Printing the table.

        cout << setprecision(2) << fixed << left << setw(20);
        cout << setw(colWidth) << "\nCelsius" << setw(colWidth) << "Fahrenheit" << setw(colWidth) << "Wind Speed" << setw(colWidth) << "Wind Chill (F)" << setw(colWidth) << "Wind Chill (C)" << endl;
        cout << setw(colWidth) << tempCelsius << setw(colWidth) << tempFahrenheit << setw(colWidth) << windSpeed << setw(colWidth) << windFahrenheit << setw(colWidth) << windCelsius << endl;

        cout << "\nIf you would like to input another set of data, please enter 'Y'" << endl;
        cin >> choice;

    }

    return 0;

}