Advertisement
Base12XB

Temperature Conversion (without Kelvin)

Oct 25th, 2014
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.81 KB | None | 0 0
  1. // Brett Westbrook
  2. // COSC 1330 - 003
  3. // Chapter 9 - Temperature Conversion
  4. // Converts from Fahrenheit to Celsius and vice versa. (Might implement Kelvin)
  5.  
  6. /*
  7.  
  8. Formulas
  9.  
  10. [°C] = ([°F] - 32) × 5/9 | FAHRENHEIT TO CELSIUS
  11. [°F] = [°C] × 9/5 + 32   | CELSIUS TO FAHRENHEIT
  12.  
  13. */
  14.  
  15. #include <iostream>
  16. #include <iomanip>
  17.  
  18. using namespace std;
  19.  
  20. char getTempType(char convertSelect);
  21. double convertToCelsius(double inFahrenheit, double outCelsius);
  22. double convertToFahrenheit(double inCelsius, double outFahrenheit);
  23.  
  24. int main()
  25. {
  26.     // Variable Declarations
  27.  
  28.     // Celsius
  29.     double inputCelsius = 0.0;
  30.     double outputCelsius = 0.0;
  31.  
  32.     // Fahrenheit
  33.     double inputFahrenheit = 0.0;
  34.     double outputFahrenheit = 0.0;
  35.  
  36.     char selection = ' ';
  37.  
  38.     selection = getTempType(selection);
  39.  
  40.     system("PAUSE");
  41.     return 0;
  42. }
  43.  
  44. // Functions
  45.  
  46. char getTempType(char convertSelect) // Processes the arithmetic and conversions
  47. {
  48.     char selection = ' ';
  49.     char loop = ' ';
  50.  
  51.     // Celsius
  52.     double inputCelsius = 0.0;
  53.     double outputCelsius = 0.0;
  54.  
  55.     // Fahrenheit
  56.     double inputFahrenheit = 0.0;
  57.     double outputFahrenheit = 0.0;
  58.  
  59.     do { // begin loop
  60.         cout << "Press 1 to convert from Celsius to Fahrenheit" << endl;
  61.         cout << "Press 2 to convert from Fahrenheit to Celsius" << endl;
  62.         cin >> selection;
  63.  
  64.         system("CLS");
  65.  
  66.         // selecting a method of conversion
  67.         switch (selection) {
  68.         case '1':
  69.             outputFahrenheit = convertToFahrenheit(inputCelsius, outputFahrenheit);
  70.             break;
  71.         case '2':
  72.             outputCelsius = convertToCelsius(inputFahrenheit, outputCelsius);
  73.             break;
  74.         default:
  75.             cout << "Invalid input." << endl;
  76.         }
  77.  
  78.         cout << endl << "Would you like to make another conversion? (Y for yes / N for no)" << endl;
  79.         cin >> loop;
  80.  
  81.         system("CLS");
  82.  
  83.         if (loop == 'n' || loop == 'N') {
  84.             cout << "Thanks for using this conversion tool!" << endl;
  85.         }
  86.     } while (loop == 'y' || loop == 'Y'); // end loop
  87.  
  88.     return selection;
  89. }
  90.  
  91. double convertToFahrenheit(double inCelsius, double outFahrenheit) // CELSIUS TO FAHRENHEIT
  92. {
  93.     // [°F] = [°C] × 9/5 + 32   | CELSIUS TO FAHRENHEIT
  94.  
  95.     double inputCelsius = 0.0;
  96.     double outputFahrenheit = 0.0;
  97.  
  98.     cout << "[Celsius] Enter temperature: ";
  99.     cin >> inputCelsius;
  100.  
  101.     outputFahrenheit = (((inputCelsius * 9) / 5) + 32);
  102.  
  103.     cout << "Fahrenheit conversion: " << outputFahrenheit << endl;
  104.  
  105.     return outputFahrenheit;
  106. }
  107.  
  108. double convertToCelsius(double inFahrenheit, double outCelsius) // FAHRENHEIT TO CELSIUS
  109. {
  110.     // [°C] = ([°F] - 32) × 5 / 9 | FAHRENHEIT TO CELSIUS
  111.  
  112.     double inputFahrenheit = 0.0;
  113.     double outputCelsius = 0.0;
  114.  
  115.     cout << "[Fahrenheit] Enter temperature: ";
  116.     cin >> inputFahrenheit;
  117.  
  118.     outputCelsius = (((inputFahrenheit - 32) / 9) * 5);
  119.  
  120.     cout << "Celsius conversion: " << fixed << setprecision(1) << outputCelsius << endl;
  121.  
  122.     return outputCelsius;
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement