-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWeatherCalculatorCPP.html
266 lines (243 loc) · 17.3 KB
/
WeatherCalculatorCPP.html
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
<!doctype html>
<html lang="en">
<head>
<link
rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/@picocss/pico@2/css/pico.min.css"
>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="color-scheme" content="dark" />
<link rel="stylesheet" href="css/pico.min.css" />
<style>
/* Reduce the container width */
.container {
max-width: 1000px; /* Adjust the value to your desired width */
}
</style>
<title>Alexander S. Corey</title>
</head>
<body>
<main class="container">
<h1><a style="text-decoration:none;" href="index.html" class="contrast">Alexander S. Corey</a></h1>
<nav>
<ul>
<li><a href="bio.html">About Me</a></li>
<li><a href="static/AlexCoreyResume.pdf" target="_blank" class="secondary">Resume</a></li>
<li><a href="https://github.com/acoreynews" target="_blank" class="secondary">GitHub</a></li>
<li><a href="https://www.linkedin.com/in/alexander-s-corey" target="_blank" class="secondary">LinkedIn</a></li>
</ul>
</nav>
<!-- HTML generated using hilite.me --><table><tr><td><pre style="margin: 0; line-height: 125%"> 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</pre></td><td><pre style="margin: 0; line-height: 125%"><span style="color: #888888">/// @author Alexander Corey</span>
<span style="color: #888888">/// @attention I pledge my word of honor that I have abided by the CSN</span>
<span style="color: #888888">/// Academic Integrity Policy while completing this assignment.</span>
<span style="color: #888888">/// @file pa06a.cpp</span>
<span style="color: #888888">/// @date 2017-10-22</span>
<span style="color: #888888">/// @brief This program prompts the user to input three weather-related values</span>
<span style="color: #888888">/// and outputs the wind chill factor and heat index.</span>
<span style="color: #888888">/// @note I spent about 6 hours developing this program. To improve code</span>
<span style="color: #888888">/// readability, I used the digit separator character ' from C++14 to break up</span>
<span style="color: #888888">/// numbers in the formulas. In addition to the textbook, I referenced</span>
<span style="color: #888888">/// cppreference.com. To help craft a function to restore a failed input</span>
<span style="color: #888888">/// stream, I referenced the clear_failure function in Kevin's lab06a solution.</span>
<span style="color: #557799">#include <iostream></span>
<span style="color: #557799">#include <cmath></span>
<span style="color: #557799">#include <iomanip></span>
<span style="color: #557799">#include <string></span>
<span style="color: #008800; font-weight: bold">using</span> <span style="color: #008800; font-weight: bold">namespace</span> std;
<span style="color: #888888">// Function prototypes</span>
<span style="color: #333399; font-weight: bold">void</span> <span style="color: #0066BB; font-weight: bold">userInput</span>(<span style="color: #333399; font-weight: bold">double</span><span style="color: #333333">&</span> degrees, <span style="color: #333399; font-weight: bold">double</span><span style="color: #333333">&</span> windSpeed, <span style="color: #333399; font-weight: bold">double</span><span style="color: #333333">&</span> humidity);
<span style="color: #333399; font-weight: bold">double</span> <span style="color: #0066BB; font-weight: bold">windChill</span>(<span style="color: #333399; font-weight: bold">double</span> degrees, <span style="color: #333399; font-weight: bold">double</span> windSpeed);
<span style="color: #333399; font-weight: bold">double</span> <span style="color: #0066BB; font-weight: bold">heatIndex</span>(<span style="color: #333399; font-weight: bold">double</span> degrees, <span style="color: #333399; font-weight: bold">double</span> humidity);
<span style="color: #333399; font-weight: bold">void</span> <span style="color: #0066BB; font-weight: bold">inputFailure</span>(istream<span style="color: #333333">&</span> cin);
<span style="color: #888888">// Const variables</span>
<span style="color: #888888">// OPENING_PROMPT holds string for prompt for user input</span>
<span style="color: #008800; font-weight: bold">const</span> string OPENING_PROMPT <span style="color: #333333">=</span> <span style="background-color: #fff0f0">"Please enter the temperature in degrees "</span>
<span style="background-color: #fff0f0">"Fahrenheit, the wind speed in miles per "</span>
<span style="background-color: #fff0f0">"hour and the humidity as a "</span>
<span style="background-color: #fff0f0">"percentage between 0 and 100."</span>;
<span style="color: #888888">// RETRY_HUMIDITY holds string for prompt for retry of user humidity input</span>
<span style="color: #008800; font-weight: bold">const</span> string RETRY_HUMIDITY <span style="color: #333333">=</span> <span style="background-color: #fff0f0">"Please re-enter the humidity as a "</span>
<span style="background-color: #fff0f0">"percentage between 0 and 100."</span>;
<span style="color: #888888">// WIND_CHILL_IS string is for final wind chill output</span>
<span style="color: #008800; font-weight: bold">const</span> string WIND_CHILL_IS <span style="color: #333333">=</span> <span style="background-color: #fff0f0">"Wind chill factor is: "</span>;
<span style="color: #888888">// HEAT_INDEX_IS string is for final heat index output</span>
<span style="color: #008800; font-weight: bold">const</span> string HEAT_INDEX_IS <span style="color: #333333">=</span> <span style="background-color: #fff0f0">"Heat index is: "</span>;
<span style="color: #888888">// main function, primary entry point for program</span>
<span style="color: #333399; font-weight: bold">int</span> <span style="color: #0066BB; font-weight: bold">main</span>()
{
<span style="color: #888888">// Local variables</span>
<span style="color: #333399; font-weight: bold">double</span> degrees <span style="color: #333333">=</span> <span style="color: #6600EE; font-weight: bold">0.0</span>; <span style="color: #888888">// holds the value of user input degrees variable</span>
<span style="color: #333399; font-weight: bold">double</span> windSpeed <span style="color: #333333">=</span> <span style="color: #6600EE; font-weight: bold">0.0</span>; <span style="color: #888888">// holds the value of user input wind mph variable</span>
<span style="color: #333399; font-weight: bold">double</span> humidity <span style="color: #333333">=</span> <span style="color: #6600EE; font-weight: bold">0.0</span>; <span style="color: #888888">// holds the value of user input humidity variable</span>
cout <span style="color: #333333"><<</span> fixed <span style="color: #333333"><<</span> setprecision(<span style="color: #0000DD; font-weight: bold">1</span>) <span style="color: #333333"><<</span> <span style="color: #0044DD">'\n'</span>;
userInput(degrees, windSpeed, humidity);
cout <span style="color: #333333"><<</span> WIND_CHILL_IS <span style="color: #333333"><<</span> windChill(degrees, windSpeed) <span style="color: #333333"><<</span> <span style="color: #0044DD">'\n'</span>;
cout <span style="color: #333333"><<</span> HEAT_INDEX_IS <span style="color: #333333"><<</span> heatIndex(degrees, humidity) <span style="color: #333333"><<</span> endl;
<span style="color: #008800; font-weight: bold">return</span> <span style="color: #0000DD; font-weight: bold">0</span>;
}
<span style="color: #888888">// userInput function takes input for weather-related variable values</span>
<span style="color: #888888">// degrees, windSpeed & humidity are passed by reference so that user</span>
<span style="color: #888888">// input values change values of variables of same name declared in main</span>
<span style="color: #333399; font-weight: bold">void</span> <span style="color: #0066BB; font-weight: bold">userInput</span>(<span style="color: #333399; font-weight: bold">double</span><span style="color: #333333">&</span> degrees, <span style="color: #333399; font-weight: bold">double</span><span style="color: #333333">&</span> windSpeed, <span style="color: #333399; font-weight: bold">double</span><span style="color: #333333">&</span> humidity)
{
cout <span style="color: #333333"><<</span> OPENING_PROMPT <span style="color: #333333"><<</span> <span style="color: #0044DD">'\n'</span>;
cin <span style="color: #333333">>></span> degrees <span style="color: #333333">>></span> windSpeed <span style="color: #333333">>></span> humidity;
<span style="color: #008800; font-weight: bold">while</span> (<span style="color: #333333">!</span>cin) {
inputFailure(cin);
userInput(degrees, windSpeed, humidity);
}
<span style="color: #008800; font-weight: bold">while</span> (humidity <span style="color: #333333"><</span> <span style="color: #0000DD; font-weight: bold">0</span> <span style="color: #333333">||</span> humidity <span style="color: #333333">></span> <span style="color: #0000DD; font-weight: bold">100</span>) {
cout <span style="color: #333333"><<</span> RETRY_HUMIDITY <span style="color: #333333"><<</span> <span style="color: #0044DD">'\n'</span>;
cin <span style="color: #333333">>></span> humidity;
}
}
<span style="color: #888888">// Function windChill uses degrees, windSpeed variables to calculate and</span>
<span style="color: #888888">// return wind chill</span>
<span style="color: #888888">// Parameters degrees, windSpeed passed by value from variables of</span>
<span style="color: #888888">// same name in main for windChill calculations</span>
<span style="color: #333399; font-weight: bold">double</span> <span style="color: #0066BB; font-weight: bold">windChill</span>(<span style="color: #333399; font-weight: bold">double</span> degrees, <span style="color: #333399; font-weight: bold">double</span> windSpeed)
{
<span style="color: #333399; font-weight: bold">double</span> windChill; <span style="color: #888888">// holds wind chill calculation to return to main</span>
windChill <span style="color: #333333">=</span> <span style="color: #6600EE; font-weight: bold">35.74</span> <span style="color: #333333">+</span> (<span style="color: #6600EE; font-weight: bold">0.6</span><span style="color: #FF0000; background-color: #FFAAAA">'</span><span style="color: #0000DD; font-weight: bold">215</span><span style="color: #333333">*</span>degrees) <span style="color: #333333">-</span> (<span style="color: #6600EE; font-weight: bold">35.75</span><span style="color: #333333">*</span>pow(windSpeed,<span style="color: #6600EE; font-weight: bold">0.16</span>)) <span style="color: #333333">+</span>
(<span style="color: #6600EE; font-weight: bold">0.4</span><span style="color: #FF0000; background-color: #FFAAAA">'</span><span style="color: #0000DD; font-weight: bold">275</span><span style="color: #333333">*</span>degrees)<span style="color: #333333">*</span>pow(windSpeed,<span style="color: #6600EE; font-weight: bold">0.16</span>);
<span style="color: #008800; font-weight: bold">return</span> windChill;
}
<span style="color: #888888">// Function heatIndex uses degrees, humidity variables to calculate and</span>
<span style="color: #888888">// and return heat index</span>
<span style="color: #888888">// Parameters degrees, humidity passed by value from variable of same name</span>
<span style="color: #888888">// in main for heatIndex calculations</span>
<span style="color: #333399; font-weight: bold">double</span> <span style="color: #0066BB; font-weight: bold">heatIndex</span>(<span style="color: #333399; font-weight: bold">double</span> degrees, <span style="color: #333399; font-weight: bold">double</span> humidity)
{
<span style="color: #333399; font-weight: bold">double</span> heatIndex; <span style="color: #888888">// holds heatIndex calculation to return to main</span>
heatIndex <span style="color: #333333">=</span> <span style="color: #333333">-</span><span style="color: #6600EE; font-weight: bold">42.379</span> <span style="color: #333333">+</span> <span style="color: #6600EE; font-weight: bold">2.04</span><span style="color: #FF0000; background-color: #FFAAAA">'</span><span style="color: #0000DD; font-weight: bold">901</span><span style="color: #FF0000; background-color: #FFAAAA">'</span><span style="color: #0000DD; font-weight: bold">523</span><span style="color: #333333">*</span>degrees <span style="color: #333333">+</span> <span style="color: #6600EE; font-weight: bold">10.14</span><span style="color: #FF0000; background-color: #FFAAAA">'</span><span style="color: #0000DD; font-weight: bold">333</span><span style="color: #FF0000; background-color: #FFAAAA">'</span><span style="color: #0000DD; font-weight: bold">127</span><span style="color: #333333">*</span>humidity <span style="color: #333333">+</span>
<span style="color: #333333">-</span><span style="color: #6600EE; font-weight: bold">0.22</span><span style="color: #FF0000; background-color: #FFAAAA">'</span><span style="color: #0000DD; font-weight: bold">475</span><span style="color: #FF0000; background-color: #FFAAAA">'</span><span style="color: #0000DD; font-weight: bold">541</span><span style="color: #333333">*</span>degrees<span style="color: #333333">*</span>humidity <span style="color: #333333">+</span>
<span style="color: #333333">-</span><span style="color: #6600EE; font-weight: bold">6.83</span><span style="color: #FF0000; background-color: #FFAAAA">'</span><span style="color: #0000DD; font-weight: bold">783</span><span style="color: #333333">*</span>pow(<span style="color: #0000DD; font-weight: bold">10</span>,<span style="color: #333333">-</span><span style="color: #0000DD; font-weight: bold">3</span>)<span style="color: #333333">*</span>pow(degrees,<span style="color: #0000DD; font-weight: bold">2</span>) <span style="color: #333333">+</span>
<span style="color: #333333">-</span><span style="color: #6600EE; font-weight: bold">5.481</span><span style="color: #FF0000; background-color: #FFAAAA">'</span><span style="color: #0000DD; font-weight: bold">717</span><span style="color: #333333">*</span>pow(<span style="color: #0000DD; font-weight: bold">10</span>,<span style="color: #333333">-</span><span style="color: #0000DD; font-weight: bold">2</span>)<span style="color: #333333">*</span>pow(humidity,<span style="color: #0000DD; font-weight: bold">2</span>) <span style="color: #333333">+</span>
<span style="color: #6600EE; font-weight: bold">1.22</span><span style="color: #FF0000; background-color: #FFAAAA">'</span><span style="color: #0000DD; font-weight: bold">874</span><span style="color: #333333">*</span>pow(<span style="color: #0000DD; font-weight: bold">10</span>,<span style="color: #333333">-</span><span style="color: #0000DD; font-weight: bold">3</span>)<span style="color: #333333">*</span>pow(degrees,<span style="color: #0000DD; font-weight: bold">2</span>)<span style="color: #333333">*</span>humidity <span style="color: #333333">+</span>
<span style="color: #6600EE; font-weight: bold">8.5</span><span style="color: #FF0000; background-color: #FFAAAA">'</span><span style="color: #0000DD; font-weight: bold">282</span><span style="color: #333333">*</span>pow(<span style="color: #0000DD; font-weight: bold">10</span>,<span style="color: #333333">-</span><span style="color: #0000DD; font-weight: bold">4</span>)<span style="color: #333333">*</span>degrees<span style="color: #333333">*</span>pow(humidity,<span style="color: #0000DD; font-weight: bold">2</span>) <span style="color: #333333">+</span>
<span style="color: #333333">-</span><span style="color: #6600EE; font-weight: bold">1.99</span> <span style="color: #333333">*</span> pow(<span style="color: #0000DD; font-weight: bold">10</span>, <span style="color: #333333">-</span><span style="color: #0000DD; font-weight: bold">6</span>)<span style="color: #333333">*</span>pow(degrees,<span style="color: #0000DD; font-weight: bold">2</span>)<span style="color: #333333">*</span>pow(humidity,<span style="color: #0000DD; font-weight: bold">2</span>);
<span style="color: #008800; font-weight: bold">return</span> heatIndex;
}
<span style="color: #888888">// In the event of a bad user input, inputFailure function takes an istream</span>
<span style="color: #888888">// variable and restores program to working order.</span>
<span style="color: #333399; font-weight: bold">void</span> <span style="color: #0066BB; font-weight: bold">inputFailure</span>(istream<span style="color: #333333">&</span> cin)
{
cin.clear();
cin.ignore(<span style="color: #0000DD; font-weight: bold">200</span>,<span style="color: #0044DD">'\n'</span>);
}
</pre></td></tr></table>
</main>
</body>
</html>