wsjlarry 发表于 2021-3-25 10:53:44

两个函数公用一个变量,提示必须初始化局部变量。但是不想输出时显示变量初...

题目:
/Computes the area of a circle and the volume of a sphere.
//Uses the same radius for both calculations.
//Code all function declarations and definitions with comments
#include<iostream>
#include<cmath>
using namespace std;
const double PI = 3.14159;
// Add function declarations here
int main()
{
int radius_of_both;
double area_of_circle, volume_of_sphere;
radius_of_both = getInput();
area_of_circle = area(radius_of_both);
volume_of_sphere = volume(radius_of_both);
return 0;
}
// Function definitions go here

正确输出:
Enter a radius to use for both a circle
and a sphere (in inches): 3
Radius = 3 inches
Area of circle = 28.2743 square inches
Volume of sphere = 113.097 cubic inches
C:\Users\Shaun\Desktop\code\middleTest\Debug\middleTest.exe (进程 21204)已退出,代码为 0。
要在调试停止时自动关闭控制台,请启用“工具”->“选项”->“调试”->“调试停止时自动关闭控制台”。
按任意键关闭此窗口. . .



我的答案:
#include<iostream>
#include<cmath>
using namespace std;

const double PI = 3.14159;
int getInput();
double area(int radius_of_both);
double volume(int radius_of_both);

int main()
{

    int radius_of_both;
    double area_of_circle, volume_of_sphere;

    radius_of_both = getInput();
    area_of_circle = area(radius_of_both);
    volume_of_sphere = volume(radius_of_both);

    return 0;

}
   
    int getInput()
{
   int radius_of_both;
   cout << "Enter a radius to use for both a circle\n"
         << "and a sphere (in inches): " << radius_of_both;
    cin >> radius_of_both;
    cout << "Radius = " << radius_of_both << " inchese\n";

    return radius_of_both;
}

    double area(int radius_of_both)
{
   double area_of_circle;
   area_of_circle = (PI * pow(radius_of_both, 2));
   cout << "Area of circle = " << area_of_circle << "\n";

   return area_of_circle;
}

    double volume(int radius_of_both)
{
   
   double volume_of_sphere;
   volume_of_sphere = ((4.0 / 3.0) * PI * pow(radius_of_both, 3));
   cout << "Volume of sphere = " << volume_of_sphere;

   return volume_of_sphere;
}

输出:
Enter a radius to use for both a circle
and a sphere (in inches):13
Radius = 3 inches
Area of circle = 28.2743 square inches
Volume of sphere = 113.097 cubic inches
C:\Users\Shaun\Desktop\code\middleTest\Debug\middleTest.exe (进程 21204)已退出,代码为 0。
要在调试停止时自动关闭控制台,请启用“工具”->“选项”->“调试”->“调试停止时自动关闭控制台”。
按任意键关闭此窗口. . .

yuxijian2020 发表于 2021-3-25 11:03:35

本帖最后由 yuxijian2020 于 2021-3-25 11:06 编辑

int getInput()
{
   int radius_of_both;
   cout << "Enter a radius to use for both a circle\n"
         << "and a sphere (in inches): "; //从这里开始注释掉 << radius_of_both;//这个变量你刚声明并没有初始化,直接输出,输出内容是不确定的
        //所以你的输出Enter a radius to use for both a circle
        //                   and a sphere (in inches):13 //->这里多了一个1
    cin >> radius_of_both;
    cout << "Radius = " << radius_of_both << " inchese\n";

    return radius_of_both;
}

wsjlarry 发表于 2021-3-25 11:09:05

谢谢大神。小弟刚学函数不久,正在努力提升。

yuxijian2020 发表于 2021-3-25 11:14:57

又是你,我说怎么好像我之前看过和这个那么像的.....

wsjlarry 发表于 2021-3-25 11:16:27

yuxijian2020 发表于 2021-3-25 11:14
又是你,我说怎么好像我之前看过和这个那么像的.....

没办法,自学成才的道路太难了。没人问
页: [1]
查看完整版本: 两个函数公用一个变量,提示必须初始化局部变量。但是不想输出时显示变量初...