【问题标题】:Input Celsius/Fahrenheit then convert输入摄氏/华氏然后转换
【发布时间】:2014-09-30 01:52:15
【问题描述】:

所以我正在编写一个 c++ 代码,该代码应该询问用户是否要使用摄氏温度或华氏温度,然后询问他们度数,然后进行转换。我的程序无法编译,并抱怨编译器需要一个标识符并且我缺少一个 ;在识别 char 之前。请详细告诉我我的代码有什么问题。请记住,我是新手,非常感谢任何信息,但我不会理解更复杂的术语/代码。

#include <iostream>
using namespace std;
int main()
{
char 'C';
double degree;
int degree_type;


cout << "What's the Degree type?: ";
cin >> degree_type;


if (degree_type == 'C')
{

cout << "What's the Temperature?: ";
cin >> degree; 

cout << degree << " degrees Celsius is = " << 9 / 5 * degree + 32 << " Degrees Fahrenheit" << endl;

}

else
{
cout << "What's the Temperature?: ";
cin >> degree;

cout << degree << " degrees Fahrenheit is = " << (degree - 32) * 5/9 << " Degrees Celsius" << endl;

}

return 0;
}

【问题讨论】:

  • char 'C'; 毫无意义。删除它。
  • 好的,这样做我可以计算出结果,这很好,但它不会让我输入可变度数。为什么不呢?
  • 在第一个 cin 之后调用 cin.ignore(); 以清除流中的换行符。

标签: c++ char type-conversion


【解决方案1】:

您必须删除 char 'C' 并将 "degree_type" 的类型更改为 char ,类似这样。

//char 'C'; Comment this line or remove
double degree;
char degree_type; // change this

【讨论】:

  • 此修复工作完美,非常感谢您的帮助。我终于明白在哪里以及如何有效地使用 char 了。再次感谢您的帮助和容忍我!
猜你喜欢
  • 2016-04-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-17
  • 2017-11-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多