steamengines

Chapter 9 homework (Temperature conversion)

Oct 30th, 2014
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.56 KB | None | 0 0
  1. //Christian Ballard
  2. //COSC 1330 - 003
  3. //This program should convert temperatures between Celsius and Fahrenheit.
  4. #include <iostream>
  5. #include <iomanip>
  6. using namespace std;
  7. //PROTOTYPES!!!!!!
  8. char getTempType(char);
  9. double convertToCelsius(double &, double &);
  10. double convertToFahrenheit(double &, double &);
  11.  
  12. int main()
  13. {
  14.     char answer = ' '; //Variables.
  15.     double celsius = 0.0;
  16.     double fahrenheit = 0.0;
  17.     double convertedCelsius = 0.0;
  18.     double convertedFahrenheit = 0.0;
  19.     char loopAnswer = ' ';
  20.  
  21.     cout << fixed << setprecision(1);
  22.    
  23.     do //AND THE LOOPIES BEGIN!!!
  24.     {
  25.         system("cls"); //I put this here to keep the loop from displaying previous output.
  26.         answer = getTempType(answer); //Assigning answer to what the user inputs in the function.
  27.  
  28.         system("cls"); //Figured it would be nice to clear the screen here.
  29.         switch (answer) //Figuring out what exactly we want to do determined by user input.
  30.         {
  31.         case '1':
  32.             convertToCelsius(celsius, convertedCelsius);
  33.             cout << "The conversion from " << celsius << ("\xF8") << " Celsius is " << convertedCelsius << ("\xF8") << " Fahrenheit." << endl << endl;
  34.             break;
  35.         case '2':
  36.             convertToFahrenheit(fahrenheit, convertedFahrenheit);
  37.             cout << "The conversion from " << fahrenheit << ("\xF8") << " Fahrenheit is " << convertedFahrenheit << ("\xF8") << " Celsius." << endl << endl;
  38.             break;
  39.         default:
  40.             cout << "Invalid input, please enter either 1 or 2." << endl;
  41.             break;
  42.         }
  43.         cout << "Would you like to convert another temperature? (Y/N)" << endl; //Update read
  44.         cin >> loopAnswer;
  45.  
  46.     }   while (loopAnswer == 'Y' || loopAnswer == 'y');
  47.     if (loopAnswer == 'n' || loopAnswer == 'N')
  48.     {
  49.         system("cls");
  50.         cout << "See ya later!" << endl;
  51.     }
  52.     system("pause");
  53.     return 0;
  54. }
  55.  
  56. char getTempType(char funAnswer)//funAnswer is my second variable for answer. It stands for Function Answer.
  57. {
  58.     cout << "To convert from Celsius to Fahrenheit please press 1." << endl;
  59.     cout << "To convert from Fahrenheit to Celsius please press 2." << endl;
  60.     cin >> funAnswer;
  61.  
  62.     return funAnswer;
  63. }
  64.  
  65. double convertToCelsius(double &funCelsius, double &funConvertedCelsius)
  66. {
  67.     cout << "Please enter the degrees Celsius" << endl;
  68.     cin >> funCelsius;
  69.     funConvertedCelsius = (funCelsius * 1.8) + 32;
  70.  
  71.     return funCelsius, funConvertedCelsius;
  72. }
  73.  
  74.  
  75.  
  76. double convertToFahrenheit(double &funFahrenheit, double &funConvertedFahrenheit)
  77. {
  78.     cout << "Please enter the degrees Fahrenheit" << endl;
  79.     cin >> funFahrenheit;
  80.     funConvertedFahrenheit = (funFahrenheit - 32) / 1.8;
  81.  
  82.     return funFahrenheit, funConvertedFahrenheit;
  83. }
Advertisement
Add Comment
Please, Sign In to add comment