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
|
// Trenton Stark
// CST-116
// Lab 2
#include <iostream>
#include <iomanip>
#include <cmath>
using std::cout;
using std::cin;
using std::endl;
using std::setw;
void inputTemp(float& cTemp, float& fTemp);
float tempConverter(float temp, bool dirc);
void inputWind(float& windS);
float windChillCalc(float fTemp, float windS);
const float fMin = -80, fMax = 121, cMin = -62, cMax = 49.5, wMin = 0, wMax = 231; //Minimum and maximum valid ranges for all user inputs
int main() {
float cTemp, fTemp, windS, windC;
cout << "Welcome to Cold Calculator v0.3" << endl;
inputTemp(cTemp, fTemp);
inputWind(windS);
windC = windChillCalc(fTemp, windS);
cout << "Calculations complete:" << endl;
cout << setw(15) << "Celcius" << setw(15) << "Fahrenheit" << setw(15) << "Wind Speed" << setw(15) << "Wind Chill" << endl;
cout << setw(15) << cTemp << setw(15) << fTemp << setw(15) << windS << setw(15) << windC << endl;
}
//Takes either celcius or fahrenheit then converts it to the other and outputs both by reference
void inputTemp(float& cTemp, float& fTemp) {
char tempType;
cout << "Would you like to enter the temperature in Celcius (c) or Fahrenehit (f)?" << endl;
do {
cin >> tempType;
cout << endl;
if (tempType != 'f' && tempType != 'c') {
cout << "Input the character 'c' or 'f'!" << endl;
}
} while (tempType != 'f' && tempType != 'c');
if (tempType == 'c') {
cout << "What is the temperature in Celcius (-62 - 49.5 degrees)?" << endl;
do {
cin >> cTemp;
cout << endl;
if (cTemp < cMin || cTemp > cMax) {
cout << "Input a value between -62 and 49.5 degrees!" << endl;
}
fTemp = tempConverter(cTemp, 0);
} while (cTemp < cMin || cTemp > cMax);
}
else {
cout << "What is the temperature in Fahrenheit (-80 - 121 degrees)?" << endl;
do {
cin >> fTemp;
cout << endl;
if (fTemp < fMin || fTemp > fMax) {
cout << "Input a value between -80 and 121 degrees!" << endl;
}
cTemp = tempConverter(fTemp, 1);
} while (fTemp < fMin || fTemp > fMax);
}
}
// Direction is 0 if going celcius to fahrenheit and 1 if going fahrenheit to celcius
float tempConverter(float temp, bool dirc) {
float cTemp;
if (dirc == 0) {
cTemp = (temp * (9.0 / 5.0)) + 32;
}
else {
cTemp = (temp - 32) * (5.0 / 9.0);
}
return cTemp;
}
void inputWind(float& windS) {
cout << "What is the wind speed in miles per hour (0 - 231)" << endl;
do {
cin >> windS;
cout << endl;
if (windS < 0 || windS > 231) {
cout << "Input a value between 0 and 231 miles per hour!" << endl;
}
} while (windS < 0 || windS > 231);
}
float windChillCalc(float fTemp, float windS) {
float windC;
windC = 35.74 + (0.6125 * fTemp) - (35.75 * pow(windS, 0.16)) + (0.4275 * fTemp * pow(windS, 0.16));
return windC;
}
|