struct pointers to functions

I'm currently stuck on a homework problem. The question asks "Create your own Date struct that contains month, date, and year information.

Create a compareDates function that takes a constant pointer to two Date variables as parameters and returns 1 if the first one is greater, -1 if the second one is greater, and 0 if they are the same.

Write a driver in main() to test your function and output the results."

I have attempted most of the problem but i'm not getting any return value. Please help.

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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>

using namespace std;

struct Date
{
	int day;
	int month;
	int year;
};

//prototype
int timeComparison(const Date *d, const Date *e);

/*************************************
* Function: main
* Description: 
**************************************/
int main()
{
	//variables
	Date d1;
	Date e1;

	Date *dPtr;
	Date *ePtr;

	dPtr = &d1;
	ePtr = &e1;

	//days = 1-30, months = 1-12, years = -5000 - 5000
	d1.day = 6, d1.month = 4, d1.year = 1990;
	e1.day = 14, e1.month = 12, e1.year = 2004;

	timeComparison(&d1, &e1);
	

	return 0;
}

/*************************************
* Function: timeComparison
* Description: 
* Input: 
* Output: 
**************************************/
int timeComparison(const Date *d, const Date *e)
{
	//variables
	int x = 1;
	int y = 0;
	int z = -1;
	
	if (d->year > e->year)
	{
		return x;
	}

	if (d->year < e->year)
	{
		return z;
	}
}
I changed my function into a void instead of an int and it seems to work. Is it still possible with the function i used above?
Line 35: You call timeComparison, but don't do anything with the result.

Line 62: The years are equal. What about checking the relationship between the months and also the days? Also, you need to return 0 if the months are equal and the days are equal.

Using the variable names x, y and z doesn't provide any clarification to the reader. Use meaningful names such as LT, EQ and GT, or don't use names at all.

Lines 25-29: What's the point of these lines? You don't use them.
OK i deleted lines 25-29. I was trying something out but forgot to delete them. I added the months and days relationship. I'm still not getting anything returned.

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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <iostream>

using namespace std;

struct Date
{
	int day;
	int month;
	int year;
};

//prototype
int timeComparison(const Date *d, const Date *e);

/*************************************
* Function: main
* Description: 
**************************************/
int main()
{
	//variables
	Date d1;
	Date e1;
	Date d2;
	Date e2;
	Date d3;
	Date e3;

	//days = 1-30, months = 1-12, years = -5000 - 5000
	d1.day = 6, d1.month = 4, d1.year = 1990;
	e1.day = 14, e1.month = 12, e1.year = 2004;

	d2.day = 29, d2.month = 12, d2.year = 2014;
	e2.day = 14, e2.month = 12, e2.year = 2014;

	d3.day = 21, d3.month = 7, d3.year = 2004;
	e3.day = 21, e3.month = 7, e3.year = 2004;

	
	timeComparison(&d1, &e1);

	timeComparison(&d2, &e2);
	timeComparison(&d3, &e3);
	

	return 0;
}

/*************************************
* Function: timeComparison
* Description: 
* Input: 
* Output: 
**************************************/
int timeComparison(const Date *d, const Date *e)
{
	//variables
	int lowerTime = 1;
	int equalTime = 0;
	int higherTime = -1;
	
	if (d->year > e->year)
	{
		return lowerTime;
	}

	if (d->year < e->year)
	{
		return higherTime;
	}

	if (d->year == e->year)
	{
		if (d->month > e->month)
		{
			return lowerTime;
		}
	}

	if (d->year == e->year)
	{
		if (d->month < e->month)
		{
			return higherTime;
		}
	}

	if (d->year == e->year)
	{
		if (d->month == e->month)
		{
			if (d->day > e->day)
			{
				return lowerTime;
			}
		}
	}

	if (d->year == e->year)
	{
		if (d->month == e->month)
		{
			if (d->day < e->day)
			{
				return higherTime;
			}
		}
	}

	if (d->year == e->year)
	{
		if (d->month == e->month)
		{
			if (d->day == e->day)
			{
				return equalTime;
			}
		}
	}
}
I think I got it!

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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <iostream>

using namespace std;

struct Date
{
	int day;
	int month;
	int year;
};

//prototype
int timeComparison(const Date *d, const Date *e);

/*************************************
* Function: main
* Description: 
**************************************/
int main()
{
	//variables
	Date d1;
	Date e1;
	Date d2;
	Date e2;
	Date d3;
	Date e3;

	//days = 1-30, months = 1-12, years = -5000 - 5000
	d1.day = 6, d1.month = 4, d1.year = 1990;
	e1.day = 14, e1.month = 12, e1.year = 2004;

	d2.day = 29, d2.month = 12, d2.year = 2014;
	e2.day = 14, e2.month = 12, e2.year = 2014;

	d3.day = 21, d3.month = 7, d3.year = 2004;
	e3.day = 21, e3.month = 7, e3.year = 2004;

	
	cout << timeComparison(&d1, &e1) << endl;
	cout << timeComparison(&d2, &e2) << endl;
	cout << timeComparison(&d3, &e3) << endl;
	
	return 0;
}

/*************************************
* Function: timeComparison
* Description: 
* Input: 
* Output: 
**************************************/
int timeComparison(const Date *d, const Date *e)
{
	//variables
	int lowerTime = 1;
	int equalTime = 0;
	int higherTime = -1;
	
	if (d->year > e->year)
	{
		return lowerTime;
	}

	if (d->year < e->year)
	{
		return higherTime;
	}

	if (d->year == e->year)
	{
		if (d->month > e->month)
		{
			return lowerTime;
		}
	}

	if (d->year == e->year)
	{
		if (d->month < e->month)
		{
			return higherTime;
		}
	}

	if (d->year == e->year)
	{
		if (d->month == e->month)
		{
			if (d->day > e->day)
			{
				return lowerTime;
			}
		}
	}

	if (d->year == e->year)
	{
		if (d->month == e->month)
		{
			if (d->day < e->day)
			{
				return higherTime;
			}
		}
	}

	if (d->year == e->year)
	{
		if (d->month == e->month)
		{
			if (d->day == e->day)
			{
				return equalTime;
			}
		}
	}
}
Lines 40-42: you're outputting a number. Not the most user friendly, but adequate to show what you got back. Did you actually check that the values were correct for the dates you passed?

Lines 61 & 67, 74 & 82, 92 & 103: You have the return values reversed. You had them correct in your first post.

Line 70, 78, 86, 97, 108: You can only get here if the years are equal. No need to test that they are equal. If > and < are both false, the only possibility is that the two values are equal.

Line 88, 99, 110: You can only get here if the months are equal. No need to test that they are equal.

Liner 108-117: You can only get here if year, month, day are all equal. Get rid of lines 108-113 and 115-117.
Topic archived. No new replies allowed.