我正在尝试编写一个程序来询问球体的半径,并根据用户输入的是体积还是表面积来确定程序将返回的内容。 (在有人说程序没有完成之前,我知道,我只是想在添加体积公式等之前让基础工作。)
不幸的是,我什至无法编译这么多,因为它似乎不承认我声明了半径。
有可能,我犯了一些粗心的错误,但毕竟,我们从错误中学习和成长。
我不想为我完成这件事,我只是想得到有关我哪里出错的指导。
#include <iostream>
#include <cmath>
using namespace std;
void sphereArea();
void sphereVolume();
double pi = 3.141596,
double radius = 0.0;
int main()
{
cout << "Please enter the radius of your sphere" << endl;
cin >> radius;
sphereArea();
system ("pause");
}
void sphereVolume(){
}
void sphereArea(){
double Vol;
Vol = ((4.0 / 3.0) * pi * (radius ^ 3));
cout << "The volume of your sphere is " << Vol << endl;
}
编辑:目前我收到这些错误: 1. 第 27 行 -> error C2296: '^': 非法,左操作数的类型为 'double' 2. 同样在第 27 行(关于半径)-> IntelliSense: expression must have integral or unscoped enum type
最佳答案
Vol = ((4.0 / 3.0) * pi * (radius ^ 3));
^
不是一种力量。这是一个位异或运算符。
您收到错误的原因是您不能对 float 进行按位运算。
将此行更改为:
Vol = ((4.0 / 3.0) * pi * (radius * radius * radius));
或者您可以使用 pow()
函数。像这样:
Vol = ((4.0 / 3.0) * pi * (std::pow(radius, 3)));
关于c++ - 用C++计算球体的体积和表面积,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34387541/