警告从'double'转换为`int'

时间:2017-02-08 19:56:16

标签: c++ visual-c++

嘿,这真的是我编过的第一件事。我想知道如何解决这个错误。我目前正在尝试做一些研究,但找不到任何有助于修复它的东西。

#include <iostream>            // needed for Cin and Cout
#include <cmath>
#include <csmath>
using namespace std;

/************************************
*     defines
*************************************/
#define  PI  3.14159

/*************************************
*     function prototype
*************************************/

int main()
{
//surface and volume

float radius; 
float height;
float surfacearea;
float volume;
int pi = 3.14159

//Get the radius
cout << "enter the radius: ";
cin >> (float)radius;

//Get height
cout << "enter height: ";
cin >> height;

//Get the surfacearea
surfacearea = 2(pi*radius^2)+2(pi*radius)* height;
cout << "The surfacearea is: " << surfacearea;

//get volume
volume = (pi*radius)^2*height;
cout << "The volume is: " << volume << endl;





system ("pause");
return 0;

}

3 个答案:

答案 0 :(得分:1)

int更改为double pi,因为pi是一个浮点数,如评论中所述,它是C ++浮点数的默认值。除非有特殊原因要使用float,否则请使用double作为浮点数。

double pi = 3.14159;

警告将消失。

此外,您无需将输入转换为float,只需:

cin >> radius;

此外,至少要将radius^2更改为radius*radius

但更好的是,完全避免使用^并使用std::pow,其中一个例子可以是found here

此外,您不需要#define PI 3.14159,因为您从未使用它,并且您尝试在pi中定义main()

答案 1 :(得分:0)

您最好在需要之前声明并初始化局部变量。对于像pi这样的常量,最好使用const和正确的类型。对于正确的类型,C ++ 11为您提供了一个很棒的工具 - auto。 ^并不代表C ++中的强大功能,而是必须使用std::pow()。所以你的代码应该是这样的:

const auto pi = 3.14159;

//Get the radius
auto radius = 0.0;
cout << "enter the radius: ";
cin >> radius;

//Get height
auto height = 0.0;
cout << "enter height: ";
cin >> height;

//Get the surfacearea
auto surfacearea = 2 * pi * pow( radius, 2.0 ) + 2 * pi * radius * height;
cout << "The surfacearea is: " << surfacearea << endl;

//get volume
auto volume = pow( pi*radius, 2.0 ) * height;
cout << "The volume is: " << volume << endl;

答案 2 :(得分:0)

首先,警告不是错误;如果它是编译错误,那么代码甚至不会编译。但是,由于它是一个警告,这意味着您的代码已经成功编译并运行,除了它在您的代码中产生了一些警告。现在你的代码中的错误:

首先,您对局部变量pi的声明不正确。 pi在您的代码中声明为数据类型 int 的变量,是 integer 的缩写。整数只是整数,正数和负数,但是一个比10 ^ 0更具特异性。现在的问题是你试图在int变量中存储一个十进制值。虽然编译器能够将十进制值转换为int值,但是会丢失该值的精度。那是因为它绕过了价值。如果您编译此示例代码:

    int floating = 1.23456789;
    cout << floating << endl;

它将输出1而不是1.23456789,原因是int变量不能存储float或double值;但它可以通过舍入它将此float或double值转换为int值。

因此,您应将pi的声明更改为:

    double pi = 3.14159; // By the way, you forgot to add a semicolon here

另一个问题:您在cin语句中使用了不必要的类型转换为半径:

    cin >> (float)radius;

如果要更改特定操作的变量的数据类型,则需要使用转换(不要更改变量数据类型;只需将其值作为数据类型转换处理。在您的情况下,它是不需要的,因为radius变量已经被声明为float的数据类型,在行中:

    float radius;

因此,我建议您只需将此cin语句更改为:

    cin >> radius;

还有一件事:代码中的以下行有问题:

    surfacearea = 2(pi*radius^2)+2(pi*radius)* height;

    volume = (pi*radius)^2*height;

“^”符号不会将数字提升为幂;它在c ++中被称为按位XOR运算符,如果它在一个操作数中设置而不是两者都设置,则它的目的是复制该位。您可以在此处找到有关它的更多信息:Bitwise Exclusive OR Operator: ^

在c ++中,如果你想将数字 x 提高到2的幂,那么你必须做x * x。或者,您可以使用pow()功能,例如:pow(x, 2.0)。对于你的代码,如果我们使用x * x方法,它就像:

    surfacearea = 2(pi*radius*radius)+2(pi*radius)* height;

    volume = (pi*radius)*(pi*radius)*height;

或者,如果我们使用pow()函数,那么代码将如下所示:

    surfacearea = 2(pi*pow(radius, 2))+2(pi*radius)* height;

    volume = pow((pi*radius), 2)*height;

修复这些问题应该可以使代码正常工作。

相关问题