C ++函数程序中的平均输出始终为0.0

时间:2018-09-08 18:13:40

标签: c++ function output average

我正在编写一个使用函数计算三个温度平均值的程序,由于某种原因,我的输出始终为0.0。我不确定这是否与变量在代码中的位置有关,或者是否应该初始化其中至少一个,但事实是我似乎无法找出问题所在。是。对于编程以及涉及函数和对象的编程,我还是很陌生的。我在这里想念什么吗?

void getTemps(double);
double calcAvg(double tempAvg);
void displayAvg();

double temp1, temp2, temp3;
double sum;
float tempAvg;

    int main()
{

    getTemps(sum);
    calcAvg(tempAvg);
    displayAvg();

    system("PAUSE");
    return 0;
}

 void getTemps(double sum)
{
    // Get up to three temperatures
        cout << "Enter temperatures of 3 cities." << endl;
        cin >> temp1;
        cin >> temp2;
        cin >> temp3;

        sum = temp1 + temp2 + temp3;
}

double calcAvg(double tempAvg)
{

    tempAvg = (sum / 3);
    return tempAvg;

}

void displayAvg()
{
    cout << fixed << setprecision(1) << temp1 << endl;
    cout << fixed << setprecision(1) << temp2 << endl;
    cout << fixed << setprecision(1) << temp3 << endl;
    cout << "The average temperature is " << tempAvg << " degrees." << endl;
}

2 个答案:

答案 0 :(得分:0)

#include <iomanip>
#include <iostream>

using namespace std;

/*
If you want to change the variable values that you pass into this function
then you will need to pass by reference using "&" when declaring the function 
params. See below:
*/

void getTemps(double & sum, double &temp1, double & temp2, double & temp3);

/*
You don't need to change the sum variable so I DONT pass the sum by 
reference here.
*/

double calcAvg(double & tempAvg, double sum);

/*
You do not need to alter any of the variables in this function, so just pass
them without the ref or with a const tag so you don't end up accidentally
altering them.
*/

void displayAvg(double tempAvg, double temp1, double temp2, double temp3);

//You should place these variables in the main method and then pass them to your functions.

//double temp1, temp2, temp3;
//double sum;
//float tempAvg;

int main(){
    double temp1, temp2, temp3;
    double sum;
    double tempAvg; //I changed this variable to a double becuase all of your functions deal with doubles and your other variables are doubles.

//All of these variables are now passed by reference, 
//so they will be altered when you change them in the function.

    getTemps(sum, temp1, temp2, temp3);
    calcAvg(tempAvg, sum);
    displayAvg(tempAvg, temp1, temp2, temp3);

    system("PAUSE");
    return 0;
}

void getTemps(double & sum, double &temp1, double & temp2, double & temp3){
// Get up to three temperatures
    cout << "Enter temperatures of 3 cities." << endl;
    cin >> temp1;
    cin >> temp2;
    cin >> temp3;

    sum = temp1 + temp2 + temp3;
}

double calcAvg(double & tempAvg, double sum){
    tempAvg = (sum / 3);
    return tempAvg;
}

void displayAvg(double tempAvg, double temp1, double temp2, double temp3){
    cout << fixed << setprecision(1) << temp1 << endl;
    cout << fixed << setprecision(1) << temp2 << endl;
    cout << fixed << setprecision(1) << temp3 << endl;
    cout << "The average temperature is " << tempAvg << " degrees." << endl;
}

答案 1 :(得分:0)

发生这种情况是因为您将全局变量发送给Function调用。所以在您的函数中创建此变量的副本。如果要使用全局变量,则答案是:

    void getTemps();

    void calcAvg();
    void displayAvg();

    double temp1, temp2, temp3;
    double sum;
    float tempAvg;

    int main()
    {

        getTemps();
        calcAvg();
        displayAvg();
        cin.get();
        cin.get();
        //system("PAUSE");
        return 0;
    }

    void getTemps()
    {
        // Get up to three temperatures
        cout << "Enter temperatures of 3 cities." << endl;
        cin >> temp1;
        cin >> temp2;
        cin >> temp3;

        sum = temp1 + temp2 + temp3;
    }

    void calcAvg()
    {

        tempAvg = (sum / 3);


    }

void displayAvg()
{
    cout << fixed /*<< setprecision(1)*/ << temp1 << endl;
    cout << fixed /*<< setprecision(1)*/ << temp2 << endl;
    cout << fixed /*<< setprecision(1*/ << temp3 << endl;
    cout << "The average temperature is " << tempAvg << " degrees." << endl;
}

但这是一种不良的编程风格。 会更好:

#include<iostream>
using namespace std;
double getTemps(double& temp1, double& temp2, double& temp3);

double calcAvg(const double sum);
void displayAvg(const double temp1, const double temp2, const double temp3, const double tempAvg);


int main()
{
    double sum=0;
    double temps1=0.f, temps2=0.f,temps3=0.f;
    double tempavg = 0.f;
    sum=getTemps(temps1,temps2,temps3);
    tempavg=calcAvg(sum);
    displayAvg(temps1,temps2,temps3,sum);
    cin.get();
    cin.get();
    //system("PAUSE");
    return 0;
}

double getTemps(double& temp1, double& temp2, double& temp3)
{
    // Get up to three temperatures
    cout << "Enter temperatures of 3 cities." << endl;
    cin >> temp1;
    cin >> temp2;
    cin >> temp3;

    return   (temp1 + temp2 + temp3);
}

double calcAvg(const double sum)
{


    return (sum / 3);

}

void displayAvg(const double temp1, const double temp2, const double temp3, const double tempAvg)
{
    cout << fixed /*<< setprecision(1)*/ << temp1 << endl;
    cout << fixed /*<< setprecision(1)*/ << temp2 << endl;
    cout << fixed /*<< setprecision(1*/ << temp3 << endl;
    cout << "The average temperature is " << tempAvg << " degrees." << endl;
}