[英]Calculate volume and surface area of sphere in C++
I am trying to write a program which asks for the radius of a sphere, and depending on if the user inputs if they want the volume or the surface area, then that's what the program will return. 我正在尝试编写一个程序,该程序要求球体的半径,并取决于用户是否输入他们想要的体积或表面积,然后程序将返回该内容。 (Before anyone says anything about the program not being done, I know, I'm just trying to get the foundation to work before I add the formula for the volume etc.) (在有人说程序未完成之前,我知道,我只是想在为体积等添加公式之前使基础工作。)
Unfortunately, I can't even get this much to compile because it doesn't seem to acknowledge that I declared the radius. 不幸的是,我什至无法编译这么多代码,因为它似乎没有承认我声明了半径。
Chances are, I made some careless mistakes, but after all, we learn and grow from my mistakes. 我可能犯了一些粗心的错误,但毕竟,我们会从错误中学习并发展。
I don't want this completed for me, I just want guidance for where I went wrong. 我不希望这一切对我来说都完整,我只是想对我哪里出错了提供指导。
#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;
}
Edit: Currently I am getting these errors: 1. On line 27 -> error C2296: '^': illegal, left operand has type 'double' 2. Also on line 27 (with regards to radius) -> IntelliSense: expression must have integral or unscoped enum type 编辑:当前,我得到以下错误:1.在第27行->错误C2296:'^':非法,左操作数的类型为'double'。2.在第27行(关于半径)-> IntelliSense:表达式必须具有整数或无作用域的枚举类型
Vol = ((4.0 / 3.0) * pi * (radius ^ 3));
^
is not a power. ^
不是力量。 It's a bit-xor operator. 这是位异或运算符。 The reason why you're getting an error is that you can't do a bitwise operation on floats. 出现错误的原因是,您无法对浮点数进行按位运算。 Change this line to this: 将此行更改为此:
Vol = ((4.0 / 3.0) * pi * (radius * radius * radius));
or you can use the pow()
function. 或者您可以使用pow()
函数。 Like this: 像这样:
Vol = ((4.0 / 3.0) * pi * (pow(radius, 3)));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.