c++ - “Int”无法转换为类型 'double'

标签 c++ int double visual-c++

我在运行时不断收到此错误:“'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,所以显示此消息。
但是,由于您要将温度从摄氏度转换为华氏度,因此最好使用 doublefloat 值而不是 int 来产生更准确和更准确的结果。有意义的输出。

关于c++ - “Int”无法转换为类型 'double',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20343771/

相关文章:

c++ - 如何调用特定于 C++ 子类的方法

c++ - 这是正确的 openMP 用法吗? (或 : can I trust the default settings? )

c - 为什么 sizeof(int) == sizeof(long)?

database - 如何在两个字段中进行搜索查询,一个是字符串上的 int

c - 双指针指向int变量覆盖内存

java - 将 double 舍入到小数点后 2 位

c++ - 如何在 FLUID gui 编辑器中旋转小部件

c++ - 在路径 : windows 中找不到 Eclipse g++

c++ - 为什么我的函数打印 '0' ?

android - 如何清空 Android 上的 gridview?