Advertisement
Guest User

First c++ program. Convert fahrenheit to celsius and reverse

a guest
Sep 28th, 2015
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.21 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. //Fahrenheit-Celcius
  6. //Convert Fahrenheit to Celsius and vice-versa
  7. //Simple menu to choose conversion and stop or run again
  8.  
  9.  
  10. void runMenu();
  11. char userInteraction();
  12. void displayResults(char menuChoice);
  13. double calcFahrtoCel(double fahrValue);
  14. double calcCeltoFahr(double celValue);
  15.  
  16.  
  17. int main()
  18. {
  19.     runMenu();
  20. }
  21.  
  22.  
  23. // Run the program, then ask to run it again
  24. void runMenu()
  25. {
  26.     displayResults(userInteraction());
  27.  
  28.     char runAgain = 'n';
  29.     cout << "Would you like to do another conversion? (y or n)" << endl;
  30.     cin >> runAgain;
  31.  
  32.     switch(runAgain)
  33.     {
  34.         case 'y':
  35.         case 'Y':
  36.         runMenu();
  37.         break;
  38.  
  39.         default:
  40.         break;
  41.     }
  42. }
  43.  
  44.  
  45. // Ask which conversion: c-f or f-c. Return char f or c
  46. char userInteraction()
  47. {
  48.     char menuChoice = 'x';
  49.  
  50.     cout << "Convert Fahrenheit to Celsius or Celsius to Fahrenheit" << endl;
  51.     cout << "Press f for Fahrenheit to Celsius" << endl;
  52.     cout << "Press c for Celsius to Fahrenheit" << endl;
  53.     cin >> menuChoice;
  54.  
  55.     return menuChoice;
  56. }
  57.  
  58.  
  59. // Take input for conversion, then run calculation
  60. void displayResults(char menuChoice)
  61. {
  62.     double fahrInput = 0.0;
  63.     double celInput = 0.0;
  64.  
  65.     switch(menuChoice)
  66.     {
  67.         case 'f':
  68.         case 'F':
  69.             cout << "Enter Fahrenheit value: " << endl;
  70.             cin >> fahrInput;
  71.             cout << fahrInput << " Fahrenheit = "
  72.                  << calcFahrtoCel(fahrInput) << " Celsius" << endl;
  73.             break;
  74.  
  75.         case 'c':
  76.         case 'C':
  77.             cout << "Enter Celsius value: " << endl;
  78.             cin >> celInput;
  79.             cout << celInput << " Celsius = "
  80.                  << calcCeltoFahr(celInput) << " Fahrenheit" << endl;
  81.             break;
  82.  
  83.         default:
  84.             cout << "Illegal option. Please press only f or c" << endl;
  85.     }
  86. }
  87.  
  88.  
  89. // Calculate Fahrenheit to Celsius
  90. double calcFahrtoCel(double fahrValue)
  91. {
  92.     double celValue = (fahrValue-32.0)*(5.0/9.0);
  93.     return celValue;
  94. }
  95.  
  96.  
  97. // Calculate Celsius to Fahrenheit
  98. double calcCeltoFahr(double celValue)
  99. {
  100.     double fahrValue = celValue*(9.0/5.0)+32.0;
  101.     return fahrValue;
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement