警告从"双精度"转换为"int"
Warning converting to `int' from `double'
嘿,这真的是我第一次编写代码。我想知道如何修复这个错误。我目前正在尝试做一些研究,但找不到任何有助于修复它的东西
#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;
}
将pi
的int
更改为double
,因为pi
是一个浮点数,正如注释中所述,它是C++对浮点数的默认值。除非有特殊原因使用float
,否则使用double
表示浮点数。
double pi = 3.14159;
警告就会消失。
此外,您不必将输入转换为float
,只需:
cin >> radius;
此外,至少将radius^2
更改为radius*radius
。
但更好的是,完全避免使用^
,而使用std::pow
,这里可以找到一个例子。
此外,您不需要#define PI 3.14159
,因为您从未使用过它,并且您尝试在main()
中定义pi
。
您最好在需要局部变量之前声明并初始化它们。对于像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;
首先,警告不是错误;如果这是一个编译错误,那么代码甚至不会编译。然而,由于这是一个警告,这意味着您的代码确实成功编译并运行了,只是它对代码中的某些内容产生了警告。现在来看看你代码中的错误:
首先,您对局部变量pi
的声明不正确。pi
在代码中声明为int数据类型的变量,integer的缩写。整数只是一个整数,有正的也有负的,但它比10^0更具体。现在的问题是,您正试图将十进制值存储在int变量中。虽然编译器能够将十进制值转换为int值,但会丢失值的精度;这是因为它四舍五入了数值。如果您编译此示例代码:
int floating = 1.23456789;
cout << floating << endl;
它将输出1而不是1.23456789,原因是int变量不能存储浮点值或双精度值;但是,它可以通过四舍五入将这个浮点值或双精度值转换为int值。
因此,您应该将pi
的声明更改为:
double pi = 3.14159; // By the way, you forgot to add a semicolon here
另一个问题:您在cin语句中对radius:使用了不必要的类型转换
cin >> (float)radius;
如果您想为特定操作更改变量的数据类型,则需要使用强制转换(您不更改变量数据类型;您只将其值处理为数据类型强制转换。在您的情况下,它是不需要的,因为radius
变量已经声明为浮点数据类型,行:
float radius;
因此,我建议您简单地将cin语句更改为:
cin >> radius;
还有一件事:代码中的以下行出现问题:
surfacearea = 2(pi*radius^2)+2(pi*radius)* height;
volume = (pi*radius)^2*height;
"^"符号不会将数字提升到幂;它在c++中被称为逐位XOR运算符,如果它设置在一个操作数中,而不是同时设置在两个操作数,则它可以用于复制位。您可以在这里找到更多信息:Bitwise Exclusive OR运算符:^
在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;
修复这些问题应该可以让代码正常工作。
- 是否可以从int转换为enum类类型
- 如何在C++中将一个无符号的 int 转换为两个无符号的短裤?
- 是什么导致了这种使用三进制而不是短整型的有符号int到无符号int转换
- 将字符串中的 int 转换为字母;
- 主.cpp:18:20:错误:从"int*"转换为"int"会失去精度 [-fa
- 将非常大的 int 转换为双倍,在某些计算机上会损失精度
- 如何将矢量<int>转换为字符数组?
- C++ 无法从 const int* 转换为 const_iterator
- 我不断收到错误 C2440'=':无法从"int *(__cdecl *)(int *,int *,int,int)"转换为"int *
- 如何将 int[4] 转换为 std::array<int,4>?
- 如何将data[i].int转换为vaible
- 将 int 转换为字符串,然后连接另一个变量以创建完整扩展名,然后将其转换为 const_char*
- 将私有矩阵形式 int 转换为双 C++
- 无法将参数 1 从 'int' 转换为 'int &'
- 将 int 转换为字符串后始终得到 0
- isspace 函数的性能警告,从 int 转换为布尔值
- 将大 int 转换为浮点数,而不舍入 c++
- 错误:无法将'uint8* {aka unsigned int*}'转换为"常量emxArray_uint8_T*"?
- 将未签名的INT转换为BCD
- 在 C++ 中将 int 转换为双精度