0

Ok, here is what I have for my compareDates() function in my class:

int Date::compareDates(int m, int d, int y)
{
	if((month < m) || (day < d) || (year < y))
		cout << "This date comes before previous valid date." << endl;
	else if((month == m) || (day == d) || (year == y))
		cout << "This date is the same as the previous valid date." << endl;
	else
		cout << "This date comes after previous valid date." << endl;

	return 0;
}

It keeps returning "This date is the same as the previous valid date". I'm assuming that it is initializing it to itself or what it just read in from the file. Any quick fixes?

2
Contributors
1
Reply
2
Views
8 Years
Discussion Span
Last Post by VernonDozier
0

Ok, here is what I have for my compareDates() function in my class:

int Date::compareDates(int m, int d, int y)
{
	if((month < m) || (day < d) || (year < y))
		cout << "This date comes before previous valid date." << endl;
	else if((month == m) || (day == d) || (year == y))
		cout << "This date is the same as the previous valid date." << endl;
	else
		cout << "This date comes after previous valid date." << endl;

	return 0;
}

It keeps returning "This date is the same as the previous valid date". I'm assuming that it is initializing it to itself or what it just read in from the file. Any quick fixes?

Why are you returning 0? If you always return the same thing, generally you should not return anything and it should be a void function. You should, when posting code, also post the CALL to the function, as this gives added information.

Here, though, the problem is visible even without the call. One, be careful regarding the difference between && and ||. && means "and", || means "or". So according to this line:

else if((month == m) || (day == d) || (year == y))

if the code gets this far, 1/1/1980 is the same as 5/1/1981 since the day is 1 for both.

But the logic is wrong for the entire function. Think about how you'd figure it out without programming. You'd check the year, then the month, then the day, in that order. Right now you have no order/precedence to your comparisons.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.