### HOW DO I MAKE THIS PROGRAM WORK?

#include <iostream>
using namespace std;

double getTemps();
double calcAvg(double temp1, double temp2, double temp3);
void displayAvg(double avg);

int main()
{
//getTemps();
calcAvg(getTemps());
displayAvg( calcAvg(temp1,temp2,temp3));

return 0;
}
double getTemps()
{
double temp1, temp2, temp3;
cout << "Enter tempertature of 3 cities" << endl;
cin >> temp1,
temp2,
temp3;
return temp1, temp2, temp3;
}
double calcAvg(double temp1, double temp2, double temp3)
{
double avg;
avg = (temp1 + temp2 + temp3) / 3;
return avg;
}
void displayAvg(double avg)
{
cout << "The average temperature is " << avg << "degrees" << endl;
}
This doesn't work:
 ``123456789`` ``````double getTemps() { double temp1, temp2, temp3; cout << "Enter tempertature of 3 cities" << endl; cin >> temp1, temp2, temp3; return temp1, temp2, temp3; }``````

The cin statement is incorrect:
 ``123`` `````` cin >> temp1, temp2, temp3;``````

it should be:
 ``123`` `````` cin >> temp1 >> temp2 >> temp3;``````

The function returns a single value of type double. If you want to return multiple values there are several possible methods. One is to pass by reference.
 ``12345`` ``````void getTemps(double& temp1, double& temp2, double& temp3) { cout << "Enter temperature of 3 cities" << endl; cin >> temp1 >> temp2 >> temp3; }``````

See tutorial on functions:
http://www.cplusplus.com/doc/tutorial/functions/
and the comma operator:
http://www.cplusplus.com/doc/tutorial/operators/

Edit: Then `main()` might look like this:

 ``12345678910`` ``````int main() { double temp1, temp2, temp3; getTemps(temp1, temp2, temp3); double average = calcAvg(temp1,temp2,temp3); displayAvg( average ); }``````

or perhaps like this:
 ``12345678`` ``````int main() { double temp1, temp2, temp3; getTemps(temp1, temp2, temp3); displayAvg( calcAvg(temp1,temp2,temp3) ); }``````

Last edited on
screams object to me.

struct tmps
{
double tmp1, tmp2, tmp3;
void getTemps() {cout << "words\n"; cin >> tmp1 >> tmp2 >> tmp3;}
};

...
tmps t; //declare an instance of your custom variable type which contains the temps here.
t.getTemps();

use(t.tmp2); //access them like this.
cout << t.tmp1;

Topic archived. No new replies allowed.