计算周长和面积而不从空函数输出值的程序

时间:2018-05-19 22:02:58

标签: c++

我为我的课程编写了这个程序,但是当我运行它时,OutputData()函数中的输出只吐出零。

我们一直在研究void函数和引用。我做了我认为我应该做的一切。他概述了我们应该设置什么。可悲的是,它不起作用,我确定我错过了一些东西。

运行时输出为零的原因是什么?

#include "stdafx.h"
#include <iostream>
#include <cmath>

using namespace std;

// Constants
const double pi = 3.14;

// Global Variables
double radius, circumference, area;

// Prototypes

void Banner();
void ComputeArea(double, double);
int  ComputeCircumference(double);
void GetValue(double);
bool GoAgain();
void OutputData(double, double, double);




int main()
{
    Banner();

    do {


        GetValue(radius);

        circumference = ComputeCircumference(radius);

        ComputeArea(radius, area);

        OutputData(radius, area, circumference);


    } while (GoAgain());

    return 0;
} // Function main()
  // ==================


  // =================
void Banner() {

    cout << "Welcome to the program!\n";
    cout << "Input a radius of a circle,\n";
    cout << "I will compute the area and\n";
    cout << "circumference of that radius.\n";
    cout << "Let's begin!\n";
} // Function Banner()
  // =========================

  // =====================
void ComputeArea(double, double) {

    area = pow((pi * radius), 2);


} // Function ComputeArea()
  // ==============================

  // =====================================
int ComputeCircumference(double
    circumference) {


    circumference = 2 * pi * radius;

    return circumference;
} // Function ComputeCircumference()
  // ======================================


  // ==========================
void GetValue(double) {

    double radius;

    cout << "Please enter your circles radius: " << endl;
    cin >> radius;


} // Function GetValue()
  // ===========================

  // =========================
bool GoAgain() {

    bool validAnswer;
    char answer;

    do {



        cout << "Enter another radius?\n";
        cout << "[y/Y] to go again. [n/N] to exit: ";
        cin >> answer;

        if ((answer == 'y') || (answer == 'y') ||
            (answer == 'n') || (answer == 'N'))
            validAnswer = true;
        else {
            validAnswer = false;
            cout << "Error. Enter a valid character: ";
        }


    } while (!validAnswer);

    if ((answer == 'y') || (answer == 'Y'))
        return true;
    else if ((answer == 'n') || (answer == 'N'))
        return false;

} // Function GoAgain()

  // ===========================

  // ===========================
void OutputData(double, double, double) {

    cout << "Here are the results: ";
    cout << "You entered: " << radius << " for the radius." << endl;
    cout << "Area: " << area << endl;
    cout << "Circumference: " << circumference << endl;

} // Function OutputData()
  // ============================

1 个答案:

答案 0 :(得分:1)

如果要传递函数的结果,有三种方法:

  1. 返回结果
  2. 通过引用(或指针)传递结果变量
  3. 使用全局变量
  4. 您的功能在其声明或定义中没有变量名称。

    注意:将值传递给而不是引用时,值会通过复制传递。修改非引用参数会修改副本而不是传递给函数的原始变量。

    返回结果

    //! Note the return type  
    double Compute_Area(double radius) // Note the parameter name
    {
      // Note the "return" statement used to return a value.
      return 2.0 * pi * radius;
    }
    

    按引用方式返回

    void Compute_Area(double radius,
                      double& area) // Note the '&' to designate reference
    {
      // The "area" variable is the parameter variable,
      //    which is the caller's variable.
      area = 2.0 * pi * radius;
    }
    

    全局变量

    double area;
    void Compute_Area(double radius)
    {
      area = 2.0 * pi * radius;
    }
    

    您应养成在函数声明和定义中提供参数名称的习惯。

    当使用多个相同类型的参数时,参数名称有助于区分参数(例如矩形区域的高度和宽度)。否则,函数的用户(包括您自己)将难以确定每个参数的用途。