我正在研究下面的程序,我希望程序做同樣的事情,但沒有一個main()函數,而是一個main()函數加一個用戶定義函數computeConeVolume,它包含計算。換句話說,我想刪除一行計算並將其替換爲一個函數調用,然後在計算中編寫main函數並將其添加,幷包含我需要的其他任何語法來完成它。錐體積計算器程序
該函數應該包含局部變量和常量聲明並且必須進行計算,它可能不會執行任何其他操作,如輸入或輸出。 應該能夠在任何地方聲明「全局」變量,但main()之外或之外沒有變量並且該函數是允許的。 應該使用值返回函數,因爲它理解起來有點簡單,但可以使用void函數。 需要在代碼頂部有一個函數原型,然後是main,然後是你的函數。
需要一些幫助,因爲我是新來的C++和試圖學習。
//Cone Volume Calculator Program
#include <iostream>
using namespace std;
int main()
{
//Declare variables and constants
double coneRadius = 0.0;
double coneHeight = 0.0;
const double PI = 3.1415;
double coneVolume = 0.0;
//Prompt the user for inputs
cout << "Enter the radius of the cone: ";
cin >> coneRadius;
cout << "Enter the height of the cone: ";
cin >> coneHeight;
//Do the calculation
coneVolume = 0.3333 * PI * coneRadius * coneRadius * coneHeight;
//Display the result
cout << "The volume of your cone is: " << coneVolume << endl;
system("pause");
return 0;
} //end of main
你有確切要求的功能,所以你應該能夠做出的第一次嘗試,並在這裏展示我們改進。如果你不知道從哪裏開始,你可以在線搜索功能教程。 – 2014-11-07 02:00:41