// Trenton Stark // CST-116 // Lab 2 #include #include #include 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 v1.0" << endl; //input inputTemp(cTemp, fTemp); inputWind(windS); windC = windChillCalc(fTemp, windS); //output cout << "Calculations complete:" << endl; cout << setw(15) << "Celcius" << setw(15) << "Fahrenheit" << setw(15) << "Wind Speed" << setw(15) << "Wind Chill" << endl; //Header column 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'); //Loops if incorrect char input 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); //Loops if input is not in range } 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); //Loops if input is not in range } } // Direction is 0 if going celcius to fahrenheit and 1 if going fahrenheit to celcius float tempConverter(float temp, bool dirc) { float conTemp; //Temperature after conversion, either celcius of fahrenheit if (dirc == 0) { conTemp = (temp * (9.0 / 5.0)) + 32; } else { conTemp = (temp - 32) * (5.0 / 9.0); } return conTemp; } //Gets wind speed and passes it back through reference 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); //Loops if input is not in range } //Converts a temperature in fahrenheit and a wind speed in miles per hour to windchill 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)); //Formula from https://www.weather.gov/media/epz/wxcalc/windChill.pdf return windC; }