Aug 30, 2017 at 3:19am UTC
#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;
}
Aug 30, 2017 at 3:59am UTC
This doesn't work:
1 2 3 4 5 6 7 8 9
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:
1 2 3
cin >> temp1,
temp2,
temp3;
it should be:
1 2 3
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.
1 2 3 4 5
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:
1 2 3 4 5 6 7 8 9 10
int main()
{
double temp1, temp2, temp3;
getTemps(temp1, temp2, temp3);
double average = calcAvg(temp1,temp2,temp3);
displayAvg( average );
}
or perhaps like this:
1 2 3 4 5 6 7 8
int main()
{
double temp1, temp2, temp3;
getTemps(temp1, temp2, temp3);
displayAvg( calcAvg(temp1,temp2,temp3) );
}
Last edited on Aug 30, 2017 at 9:57am UTC
Aug 30, 2017 at 1:58pm UTC
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;