Return Question??

Here is the code I have for get_avg(), obviously it does not return both avg_temp_hi and avg_temp_lo. How can I do this? Is there an easier way than making a structure? If not, how and where would I set up the structure? Thanks for the help.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
float DailyTemps::get_avg()
{
float avg_temp_hi;
float avg_temp_lo;
float total_sum_hi;
float total_sum_lo;

total_sum_hi = 0;
total_sum_lo = 0;

for(int i = 0; i < num_days; i++)
{
	total_sum_hi += hi_temps[i];
	total_sum_lo += lo_temps[i];

	avg_temp_hi = total_sum_hi / num_days;
	avg_temp_lo = total_sum_lo / num_days;
}
    return(avg_temp_hi, avg_temp_lo);
}

I need to call both of these averages in a cout in main() so how would I do that as well? THANKS A LOT FOR THE HELP! I really appreciate it.
You could use pointers:

example:
1
2
3
4
5
6
7
8
9
10
11
12
13
void test(int * A, int * B)
{
  *A = 123;
  *B = 456;
}

int main()
{
  int x = 0;
  int y = 0;
  test(&x,&y);
  cout << x << y;
}


123456

Last edited on
Change all floats to doubles. They are built for the computer better than floats.

1
2
	total_sum_hi += hi_temps[i];
	total_sum_lo += lo_temps[i];  //these are not arrays, so you'll get an error 


You never declared num_days.

You can't return two variables, for that you'll need a void function.
http://www.cplusplus.com/doc/tutorial/functions/
http://www.cplusplus.com/doc/tutorial/functions2/

"::" implies a namespace, you don't have a namespace DailyTemps.

Are you learning programming on your own?
@GRex2595 say what?

"::" implies namespace?? Maybe this funtion is a class member and num_days, lo_temps and hi_temp are class variables...
Sorry. I never actually read on classes so I didn't see that.
Last edited on
I know namespaces, but it not the only place where "::" is used...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class test{
public:
  void init();
};

void test::init()
{
  // do something
}

int main()
{
  test a;
  a.init();
  return 0;
}
Jikax is right. No, I am not learning on my own. And yes, this function is a class member. I declared num_days in another part of the program GRex. The entire program compiles and runs except for this part which I can't figure out how to do. So declarations are not my problem here. I did not present the entire program but I can if you guys think it would help.
And they are definitely arrays GRex, and I do not get errors.
This is a bit off topic don't you think? Let's get back to your return values... Did my advice help?
I'm totally sorry. It looked like a self-taught thing and I haven't used classes yet so I didn't know :: was for the class. Your code didn't show arrays, so I thought that it was just regular ints. I get the classes a little bit better now though. Thanks
You could so something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <utility>


std::pair<float,float> Daily::get_avg()
{
	float avg_temp_hi;
	float avg_temp_lo;
	float total_sum_hi;
	float total_sum_lo;

	total_sum_hi = 0;
	total_sum_lo = 0;

	for(int i = 0; i < num_days; i++)
	{
		total_sum_hi += hi_temps[i];
		total_sum_lo += lo_temps[i];

		avg_temp_hi = total_sum_hi / num_days;
		avg_temp_lo = total_sum_lo / num_days;
	}
	return std::pair<float,float>(avg_temp_hi, avg_temp_lo) ;
//	return(avg_temp_hi, avg_temp_lo);
}

// then somewhere in code:

	std::pair<float, float> average = x.get_avg() ;
	std::cout << "Highest average temp: " << average.first ;
	std::cout << "\nLowest average temp: " << average.second ;


You could also make it a bit less cumbersome by putting: typedef std::pair<float,float> average; somewhere in your class definition (in a public: area) so all of those std::pair<float,float> in the above code would, instead, read DailyTemp::average
Last edited on
above the class definition:

1
2
3
4
struct avgTemps {
   double mTempHi;
   double mTempLo;
};


in the class (private part):

avgTemps mAvgTemps;

Then in the method:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
avgTemps DailyTemps::get_avg()
{
   double total_sum_hi = 0;
   double total_sum_lo = 0;
   int i;

   for(i = 0; i < num_days; i++)
   {
	total_sum_hi += hi_temps[i];
	total_sum_lo += lo_temps[i];

	this->mAvgTemps->mTempHi = total_sum_hi;
	this->mAvgTemps->mTempLo = total_sum_lo;
   }
   return this->mAvgTemps;
}


Note that I have changed all your floats to doubles - it is more precise than float.
Also note that the method now returns avgTemps instead of float (or double).
Last edited on
Why use the variables at all?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
AVG_TEMPS DailyTemps::get_avg()
{
   AVG_TEMPS temps = { 0,};

   for(int i = 0; i < num_days; i++)
   {
	temps.mTempHi += hi_temps[i];
	temps.mTempLo += lo_temps[i];
   }

   temps.mTempHi = temps.mTempHi / num_days;
   temps.mTempLo = temps.mTempLo / num_days;

   return temps;
}
Last edited on
berzerger I tried what you said but I get an error that says prototype for 'avgTemps DailyTemps:: get_avg()' does not match any in class 'DailyTemps'.

Here is my class:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

struct avgTemps {
   double mTempHi;
   double mTempLo;
};

class DailyTemps
{
public:
DailyTemps();
int	get_hi(int day);
int get_lo(int day);
float get_avg(int day);
void	set_temp_hi(int day, int temp);
void	set_temp_lo(int day, int temp);
int	get_hi();
int	get_lo();
float get_avg();

private:
ifstream temps_in;
int	 hi_temps[MAX_DAYS], lo_temps[MAX_DAYS];
int	 num_days;
avgTemps mAvgTemps;

};
Change float get_avg(); to avgTemps get_avg();. :)
Topic archived. No new replies allowed.