0

I'm writing a program that calculates the average of three temperatures using functions, and for some reason my output is always 0.0 in it. I'm not sure if it has something to do with the placement of the variables inside the code, or if I'm supposed to initialize at least one of them, but the fact is I can't seem to find out what the problem is. I'm still pretty new to programming, and to programming that involves functions and objects. Is there something that I'm missing here?

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;
}
  • 1
    You need to pass `sum` and `tempAvg` by reference, not by value. `void getTemps(double& sum)` and `void calAvg(double& tempAvg)`. There are probably duplicates of this question on SO. – R Sahu Sep 08 '18 at 18:15
  • 2
    You need to figure out whether you want to use global variables or parameter passing. – melpomene Sep 08 '18 at 18:17
  • Unrelated: Probably no point to switching from `double` for everything else to `float` for `tempAvg`. Might as well make them all `double`s. – user4581301 Sep 08 '18 at 18:18
  • 1
    Avoid global variables whenever you can. Declare them where you need them. – Swordfish Sep 08 '18 at 18:19
  • 1
    You are guessing how to do things. Don't. Start with these [C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). C++ can't be learned by guessing. – Ron Sep 08 '18 at 18:19
  • look up the "return" keyword – Kenny Ostrom Sep 08 '18 at 18:43

2 Answers2

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;
}
CalebSE
  • 91
  • 6
0

This happens because you send global variable to Functions call. So in your function creates copy of this variables. If you want use global var answer is:

    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;
}

But this is a bad programming style. Would be better :

#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;
}