【问题标题】:c++ Fahrenheit or Celsius converterc++华氏或摄氏转换器
【发布时间】:2016-09-21 20:13:08
【问题描述】:

嗨,只是想知道为什么这不起作用,到目前为止,无论我选择 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;
   }

【问题讨论】:

标签: c++


【解决方案1】:

这可能会更好:

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

【讨论】:

  • 评论不够吗?这与问题标题有何关系?
  • 谢谢我的朋友
  • 它表明发布的代码并没有像 OP 认为的那样。
  • if (toupper(unit) == 'C')。甚至if (strchr("Cc", unit)).
  • 是的,这些都会更好。
猜你喜欢
  • 1970-01-01
  • 2016-04-14
  • 2021-06-17
  • 1970-01-01
  • 2017-11-30
  • 2018-12-16
  • 2017-07-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多