#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
//Declares a variable named name that holds text.
string name;
//Declares variables named height and radius that hold whole numbers.
int height, radius;
//Declares a variable named volume that holds double precision real numbers.
double volume;
//Declare a constant variable for PI
const double PI = 3.1415;
//Declare input file
ifstream inFile;
//open file
inFile.open("input4.txt");
//Prompts the user "May I get your full name please?: ".
cout << "May I get your full name please?: " << endl;
//Reads the value from the keyboard and stores it in name.
getline( cin, name);
//Prompts the user "Thanks ", name, " , now enter height and radius of the cone please: ".
cout << "Thanks " << name << ", now enter height and radius of the cone please: " << endl;
//Reads the values from the keyboard and stores them in height and radius respectively.
cin >> height;
cin >> radius;
//Calculates the volume using the formula volume = 1.0/3 x 3.1415 x radius x radius x height
volume = 1.0/3 * PI * radius * radius * height;
//Rounds the volume to the nearest tenths (one decimal digit) and reassigns it to volume.
volume = static_cast<double>(static_cast<int>((volume * 10.0) + 0.5))/10.0;
//Print final message
cout << "Ok " << name << " the cone's volume is " << volume << endl;
inFile.close();
return 0;
}
一旦我按下Enter键获取我的音量,调试实验室就会退出,然后才能得到答案。我只能通过录制屏幕并播放来查看我的答案。欢迎任何帮助。谢谢。
答案 0 :(得分:1)
替换
volume = 1/3 * PI * radius * radius * height;
带
volume = 1.0/3 * PI * radius * radius * height;
两个整数的除法导致整数结果。 1/3 = 0余数1,因此整个表达式设置为0.将一个或两个操作数更改为浮点将导致浮点结果。