c ++ Fahrenheit或Celsius转换器

时间:2016-09-21 20:13:08

标签: c++

嗨只是想知道为什么这不起作用,因为现在它打印出华氏温度,无论我选择c还是f。请帮忙

#include <iostream>
using namespace std;

int main()
{

    char unit;
    float degrees = 0.0;
    float Farenheit, Celsius;

   cout << "Enter the temperature unit you are currently in (f or c): ";
   cin >> unit;
   cout << "Enter the temperature in degrees: ";
   cin >> degrees;

   if ( unit == 'c' || 'C')
   {
      Farenheit = (degrees - 32) / 9 * 5.0;
      cout << "The degrees in Farenheit are: " << Farenheit << endl;
   }

   else if ( unit == 'f' || 'F')
   {
      Celsius = (degrees - 32) * 5.0/9;
      cout << "The degrees in Celsius is: " << Celsius << endl;
   }

   return 0;
   }

1 个答案:

答案 0 :(得分:3)

这可能会更好:

if ( unit == 'c' || unit == 'C')