我在运行时不断收到此错误:“'Int'不可转换为'double'类型”,它在我运行程序时立即显示此错误,但随后它很快消失,然后显示我的程序。我用的是VC2010。 (编辑:这个程序是将摄氏度转换为华氏度,并判断它是否热。)
#include <iostream>
int convert(int);
int main(void)
{
using std::cout;
using std::cin;
using std::endl;
cout << "Enter the degrees in Celsius: ";
int temp;
cin >> temp;
int degrees = convert(temp);
if(degrees<100)
{
cout << degrees << " is not too hot." << endl;
}
else if(degrees>100)
{
cout << degrees << " is hot." << endl;
}
cin.get();
cin.get();
return 0;
}
int convert(int ctf)
{
return ctf * 1.8 + 32;
}
最佳答案
您应该将 convert
方法的结果显式转换为 int 以避免出现此消息。
int convert(int ctf)
{
return (int) (ctf * 1.8 + 32);
}
由于返回类型指定为int
,但浮点乘法的结果不是int
,所以显示此消息。
但是,由于您要将温度从摄氏度转换为华氏度,因此最好使用 double
或 float
值而不是 int
来产生更准确和更准确的结果。有意义的输出。
关于c++ - “Int”无法转换为类型 'double',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20343771/