Outputting a called Function

Hey guys I have a function tomorrow(); in a Date.cpp class that I would like to call in my Employee.cpp class. The Function looks like this
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
  void Date::tomorrow()
{
	{
	int m = month, d = day, y = year;

	switch(m)


	{case 1: case 3: case 5: case 7: case 8: case 10: case 12:
		if(d < 30)
		{d ++; break;}

		else if((d <= 31) && (m < 11))
		{m ++;d = 1;break;}

		else if((d <= 31) && (m >= 12))
		{y ++;m = 1;d = 1;break;}


	case 4: case 6: case 9: case 11:
		if(d < 29)
		{d ++;break;}

		else if(d <= 30)
		{m ++;d = 1;break;
		}


	case 2: //February
		if((leapyear(y)) && (d < 29))
		{d ++;break;}

		else if((!(leapyear(y))) && (d < 28))
		{d ++;break;}

		else if((leapyear(y)) && (d >= 29))
		{m ++;d = 1;break;}

		else if ((!(leapyear(y))) && (d >= 28))
		{m ++; d = 1; break;}


	}
	cout << m << "-" << d << "-" << y << endl;
	}
}


I called it in my Employee.cpp as follows to add a probationary date of 90 days.

1
2
3
4
5
6
7
8
9
10
void Employee::calc_probationary_date()
{
	
	for (int i = 0; i < 90; i++)
	{
		void tomorrow();

// how save the date and output it?
	}
}


I need to save the date after it loops 90 times. How do I do that? Thanks
1
2
3
4
5
6
7
8
9
10
void Employee::calc_probationary_date()
{
	
	for (int i = 0; i < 90; i++)
	{
		void tomorrow();

// how save the date and output it?
	}
}

You can use '&' and pointer to save the value in void.
If you don't want to use them, then you should not use void, because using other datatypes (eg. int,double,float,bool) you can return something.
Ahhh you're right, that a great idea. Sorry I'm a novice programmer. What would that code look like? Where would the pointer go in that?
m, d and y are local variables within Tomorrow. They go out of scope when Tomorrow exits.

What about changing Tomrrow to return a Date instance by value?
1
2
3
4
5
Date Date::tomorrow()
{ 
...
  return Date (y, m,d);
}

This assumes you have a Date constructor that takes year, month and day as arguments.

BTW, in the following snippet, line 3 is not a function call. It is a function declaration. Remove the void.

1
2
3
4
5
void Employee::calc_probationary_date()
{  for (int i = 0; i < 90; i++)
    {  void tomorrow();  // This is a function declaration
    }
}


Last edited on
aesopq122333 wrote:
You can use '&' and pointer to save the value in void.

You can't store anything in a void. You can use a void pointer, but that is a poor practice. It gives up type safety.
Ugh this isn't working correctly, thank you, I didn't realize I had void there!

But I cannot change anything on tomorrow(); This is for a class and I have spent hours on this and cannot figure out how to store it without changing tomorrow.
If you can't change the declaration of Tomorrow, then you need to get rid of m, d, y (line 4) and operate directly on year, month and day.

Or ugly shortcut:
1
2
3
4
5
6
 
//  After line 45
//  Change the instance variables
  year = y; 
  month = m;
  day = d;

Last edited on
I got it! Thanks Abstract, you've helped a lot! Have a good day :D
Topic archived. No new replies allowed.