gpt4 book ai didi

c++ - 警告从 `int' 转换为 `double'

转载 作者:行者123 更新时间:2023-11-28 05:18:23 26 4
gpt4 key购买 nike

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

#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;

}

最佳答案

更改 intdouble对于 pi ,因为 pi是一个 float ,如注释中所述,它是 C++ 的默认 float 。除非有特殊原因使用 float , 使用 double用于 float 。

double pi = 3.14159;

警告将消失。

此外,您不必将输入转换为 float ,简单地说:

cin >> radius;

另外,至少,改变radius^2radius*radius .

但更好的是,避免 ^一起使用std::pow , 一个例子可以是 found here .

此外,您不需要 #define PI 3.14159因为您从不使用它,而您尝试定义 pimain() .

关于c++ - 警告从 `int' 转换为 `double',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42122434/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com