KySoto

Converter.cpp

Aug 27th, 2013
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.25 KB | None | 0 0
  1. /*we does the F to C conversions, gunna add the C back to F as well.
  2. mathew myers
  3. i know this "could" have been simpler but i gotta "challenge" myself a little bit so i went for a little bit more functionality*/
  4. #include<iostream>
  5. #include<string>
  6. double converter(bool b, double t);
  7. void main()
  8. {
  9.     using namespace std;//declairations
  10.     double t = 0;
  11.     string s = "";
  12.    
  13.     cout << "Please enter f if you want to use the Fahrenheit to Celcius converter, to use the Celcius to Fahrenheit converter just press enter.\n";
  14.     cin >> s;
  15.     if( s == "f")//yay you get to choose between converters
  16.     {
  17.         cout << "Please enter the temperature in Fahrenheit.\n";
  18.         cin >> t;
  19.         cout << "The temperature in Celcius is: " <<converter(true,t) << "\n";
  20.     }
  21.     else
  22.     {
  23.         cout << "Please enter the temperature in Celcius.\n";
  24.         cin >> t;
  25.         cout << "The temperature in Fahrenheit is: " <<converter(false,t) << "\n";
  26.     }
  27. }
  28. /*the formula used/
  29. °C  x  9/5 + 32 = °F
  30. (°F  -  32)  x  5/9 = °C */
  31. double converter(bool b, double t)
  32. {
  33.     if ( b == true) //true means the F to C and false means C to F
  34.     {
  35.         return ((t - 32) * 5.0/9.0); //might as well save lines of code and space and just have the entire calculation on the return.
  36.     }
  37.     else
  38.     {
  39.         return (t * 9.0/5.0 + 32);
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment