The output is not coming out correctly.

Hey guys I have a program that takes three temps from the user and calculates the average for them. But the only issue i have is that when i add in the temps the average comes out as zero I have no clue whats wrong any help would be appreciated.

Heres the program:

#include <iostream>
#include <iomanip>
using namespace std;
void getTemps(double temperature1, double temperature2, double temperature3);
double calcAvg(double temperature1, double temperature2, double temperature3);
void displayAvg(double avgTemp, double temperature1, double temperature2, double temperature3);

int main()
{
double temperature1 = 0;
double temperature2 = 0;
double temperature3 = 0;
double avgTemp = 0;
getTemps(temperature1, temperature2, temperature3);
avgTemp = calcAvg(temperature1, temperature2, temperature3);
displayAvg(avgTemp, temperature1, temperature2, temperature3);

return 0;
}

void getTemps(double temperature1, double temperature2, double temperature3)
{

cout << "Enter temperature of 3 cities.\n";
cout << "#1: ";
cin >> temperature1;
cout << "#2: ";
cin >> temperature2;
cout << "#3: ";
cin >> temperature3;
}

double calcAvg(double temperature1, double temperature2, double temperature3)
{
double avgTemp = 0;

avgTemp = (temperature1 + temperature2 + temperature3) / 3.0;
return avgTemp;

}

void displayAvg(double avgTemp, double temperature1, double temperature2, double temperature3)

{
cout << fixed << setprecision(1) << showpoint << endl;

cout << fixed << setprecision(1) << showpoint << endl;

cout << endl << setprecision(1) << showpoint << endl;
cout << "The average temperature is " << avgTemp << fixed << setprecision(1) << showpoint << " degrees.\n";
}

===============================================================================
Heres the output I get :

Enter temperature of 3 cities.
#1: 98
#2: 79
#3: 87




The average temperature is 0.0 degrees.
In function getTemps(), you need to pass the parameters by reference, instead of by value,

 
void getTemps(double& temperature1, double& temperature2, double& temperature3)


See tutorial for explanation:
http://www.cplusplus.com/doc/tutorial/functions/
I already tried that Im running off a mac and it does not recognize &
The odds that you somehow got a C++ compiler that doesn't recognise "&" are basically zero. You must have typed it in wrong.
I already tried that Im running off a mac and it does not recognize &


Well, the Mac keyboard recognised it enough to type your reply.

I agree with @Repeater. Please show the code that you believe doesn't compile with &. Preferably in code tags.

Remember that you will have to use & in both prototype and function definition (so, in two lines of your code).
Last edited on
Well I feel really stupid got it working thanks guys.......
Topic archived. No new replies allowed.