Guest User

Untitled

a guest
Apr 23rd, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.19 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. void controller();
  6. float ctof(float);
  7. float ftoc(float);
  8.  
  9. int main()
  10. {
  11.     controller();
  12.     return 0;
  13. }
  14.  
  15. void controller()
  16. {
  17.     char yesno = 'n';
  18.     float temperature[10];
  19.  
  20.     cout << "Would you like to convert Celsius to Fahrenheit(y/n): ";
  21.     while(yesno != 'y' || yesno != 'n')
  22.     {
  23.         cin >> yesno;
  24.         if(yesno == 'y') { // If yes then convert to fahrenheit
  25.             cout << "Celsius to fahrenheit it is then!\n\n";
  26.             break;
  27.         }
  28.         else if(yesno == 'n') {
  29.             cout << "OK! Fahrenheit to Celsius then.\n\n";
  30.             break;
  31.         }
  32.         else cout << "Invalid input, enter either y or n.";
  33.     }
  34.  
  35.     for(int i = 0; i < 10; i++)
  36.     {
  37.         cout << "Please enter temperature number " << i+1 << ": ";
  38.         cin >> temperature[i];
  39.     }
  40.  
  41.     cout << endl << "Now for the conversion..."<< endl;
  42.  
  43.     for(int i = 0; i < 10; i++)
  44.     {
  45.         float temp;
  46.         if(yesno == 'y')
  47.             temp = ctof(temperature[i]);
  48.         else temp = ftoc(temperature[i]);
  49.         cout << "Temperature " << i+1 << ": " << temp << endl;
  50.     }
  51. }
  52.  
  53. float ctof(float cels)
  54. {
  55.     return (cels/5)*9+32; // The celsius to fahrenheit formula
  56. }
  57.  
  58. float ftoc(float fah)
  59. {
  60.     return (fah-32)/9*5; // The fahrenheit to celsius formula
  61. }
Add Comment
Please, Sign In to add comment