鱼C论坛

 找回密码
 立即注册
查看: 1057|回复: 4

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

[复制链接]
发表于 2021-3-25 10:53:44 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
题目:
/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。
要在调试停止时自动关闭控制台,请启用“工具”->“选项”->“调试”->“调试停止时自动关闭控制台”。
按任意键关闭此窗口. . .
最佳答案
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;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 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;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-3-25 11:09:05 | 显示全部楼层
谢谢大神。小弟刚学函数不久,正在努力提升。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-3-25 11:14:57 | 显示全部楼层
又是你,我说怎么好像我之前看过和这个那么像的.....
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-3-25 11:16:27 | 显示全部楼层
yuxijian2020 发表于 2021-3-25 11:14
又是你,我说怎么好像我之前看过和这个那么像的.....

没办法,自学成才的道路太难了。没人问
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-22 01:14

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表