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
|
// Trenton Stark
// CST-116
// Lab 2
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
float tempConverter(float temp, bool dirc);
void inputTemp(float& cTemp, float& fTemp);
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;
cout << "Welcome to Cold Calculator v0.2" << endl;
inputTemp(cTemp, fTemp);
cout << cTemp << endl;
cout << fTemp << endl;
}
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;
}
|