Source of ftemp_to_ctemp2.cpp


  1: //ftemp_to_ctemp2.cpp
  2: //Converts a Fahrenheit temperatures to Celsius.

  4: #include <iostream>
  5: using namespace std;

  7: int CelsiusTemp(double fahrenheit);


 10: int main()
 11: {
 12:     cout << "\nThis program converts a Fahrenheit temperature to "
 13:         "an equivalent Celsius value.\nBoth values are reported in "
 14:         "rounded form.\n\n";

 16:     double fTemp;
 17:     cout << "Enter your Fahrenheit value here: ";
 18:     cin >> fTemp;  cin.ignore(80, '\n');

 20:     //Round the Fahrenheit temperature to the nearest integer
 21:     if (fTemp >= 0)
 22:         fTemp = int(fTemp + 0.5);
 23:     else
 24:         fTemp = int(fTemp - 0.5);

 26:     cout << "\nFahrenheit Temperature:   " << fTemp
 27:          << "\nEquivalent Celsius Value: " << CelsiusTemp(fTemp);
 28:     cout << endl << endl;
 29: }


 32: int CelsiusTemp(/* in */ double fahrTemp)
 33: //Pre:  "fahrTemp" contains a valid Fahrenheit temperature,
 34: //      which may be an integer or real quantity
 35: //Post: Return-value of the function is the equivalent Celsius value,
 36: //      rounded to the nearest integer
 37: {
 38:     const double C_DEGREE_PER_F_DEGREE = double(5) / double(9);

 40:     double cTemp = (fahrTemp - 32) * C_DEGREE_PER_F_DEGREE;

 42:     if (cTemp >= 0)
 43:         return int(cTemp + 0.5);
 44:     else
 45:         return int(cTemp - 0.5);
 46: }