summaryrefslogtreecommitdiff
path: root/CST116-Lab2-Florea/CST116-Lab2-Florea.cpp
blob: e1eebc98262c94df80abbc88fc50994183baf643 (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
/* 
Andrei Florea - CST 116 - Lab 2 - Calculating wind chill

This program takes in wind speed in MPH and temperature in either Fahrenheit or Celsius
It will return the calculated wind chill using those variables
*/

#include <iostream>
#include <math.h>

using std::cout;
using std::cin;
using std::endl;

void user_entry_temp(float& temperature, char decision);
void user_entry_speed(float& wind_speed);
float convert_to_f(float c_temp);
float calculate_wind_chill(float temp, float wind);

const int F_MAX = 121;
const int F_MIN = -80;

const float C_MAX = 42.5;
const int C_MIN = -62;

const int W_MAX = 231;
const int W_MIN = 0;

int main()
{
    // Driver function for the program
    float temperature = -100;
    float wind_speed = -100;
    float chill;
    char temp_decision;

    cout << "Do you want to input temperature in Fahrenheit (F) or Celsius (C)?" << endl;
    cin >> temp_decision;
    temp_decision = tolower(temp_decision);


    user_entry_temp(temperature, temp_decision);
    user_entry_speed(wind_speed);


    chill = calculate_wind_chill(temperature, wind_speed);
    temp_decision = toupper(temp_decision);

    cout << "For " << temperature << " degrees " << temp_decision << " and " << wind_speed << " mph";
    cout << " the wind chill is: " << chill << endl;


}

void user_entry_temp(float& temperature, char decision)
{
    // Takes argument by reference and limits the input to a specific range for temperature, doesn't return, it changes by reference

    if (decision != 'f' && decision != 'c') // Ensures that the temp_decision is either f or c.
    {
        while (decision != 'f' && decision != 'c')
        {
            cout << "Wrong input for temperature, do you want temperature in Fahrenheit (F) or Celsius (C)?" << endl;
            cin >> decision;
            decision = tolower(decision);
        }
    }

    if (decision == 'c')
    {
        while (temperature < C_MIN || temperature > C_MAX) // Ensures input for temperature is within the range
        {
            cout << "Enter a value in Celsius for temperature (-62C - 42.5C inclusive): " << endl;
            cin >> temperature;
        }

        cout << "You entered " << temperature << " degrees celsius." << endl;
        temperature = convert_to_f(temperature); // Calls to convert temperature to Fahrenheit 

    }
    else if (decision == 'f')
    {
        while (temperature < F_MIN || temperature > F_MAX) // Ensures input for temperature is within the range
        {
            cout << "Enter a value in Fahrenheit for temperature (-80F - 121F inclusive): " << endl;
            cin >> temperature;
        }
        cout << "You entered " << temperature << " degrees Fahrenheit." << endl; 
    }
}

void user_entry_speed(float& wind_speed)
{
    // Takes user input for wind_speed and makes sure its within the given range, doesn't return it changes by reference

    while (wind_speed < W_MIN || wind_speed > W_MAX)
    {
        cout << "Enter a speed for the wind in MPH (0 - 231, inclusive): " << endl;
        cin >> wind_speed;
    }
    cout << "You entered " << wind_speed << " miles per hour." << endl;
}

float convert_to_f(float c_temp)
{
    // Takes input in celsius, and will convert it to Fahrenheit, returns converted temp
    c_temp = 1.8 * c_temp + 32;
    return c_temp;
}

float calculate_wind_chill(float temp, float wind)
{
    // Takes the temperature and wind speed and calculates chill, returns float
    return 35.74 + .6215 * temp - 35.75 * pow(wind, .16) + .4275 * temp * pow(wind, .16);
}