Date comparsion problem

Write your question here.

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
  #include "stdafx.h"
#include <iostream>
using namespace std;

class Date
{
private:
	int Day, Month, Year;
public:
	Date(int InputDay, int InputMonth, int InputYear):
		Day(InputDay), Month (InputMonth), Year(InputYear) {};
	bool operator< (const Date& compare)
	{ 
		if (Year< compare.Year)
			return true;
		
		else if (Month < compare.Month)
			return true;
		else if (Day < compare.Day)
			return true;
		else 
			return false;}
	void Display()
	{
		cout<< Day <<"/"<<Month<<"/"<< Year<<endl;
	}
};

int main()
{
	Date Holy1 (25,2, 2088);
	Date Holy2 (15, 10,2019);

	cout<<"Holy1 is ";
	Holy1.Display();
	cout<<"Holy2 is ";
	Holy2.Display();

	if (Holy1 < Holy2)
		cout<<"Holiday 1 happens FIRST"<<endl;

	else if (Holy2 < Holy1)	
		cout<<"Holiday 2 happens FIRST"<<endl;

	return 0;

}



Hi ,friends.... when l compile this program, the result is "Holiday 1 happens FIRST".... This is obviously not the truth! Because 2019 should go before 2088.
The compiler simply cannot "look at the same level" to check the "Year" for (Holy2 <Holy1)....

l think the problem is in the implementation of "bool operator <" overloading.
Because when it checks the Year(2088>2019), it would not cout"Holiday 2 happens
First" , rather it just go on to check the month(2<10), so it prints "Holiday 1 happens FIRST" but that is wrong.

As l am a very beginner of C++, l don't know how to fix it... Please help!
1
2
3
4
5
6
   bool operator< (const Date& compare)
   { 
      if ( Year  != compare.Year ) return Year  < compare.Year ;
      if ( Month != compare.Month) return Month < compare.Month;
      return Day < compare.Day;
   }
Last edited on
Date has actually three possible cases:
1. lhs.Year < rhs.Year the lhs is clearly younger
2. lhs.Year > rhs.Year the rhs is clearly younger
3. lhs.Year == rhs.Year one must compare the months

Your tests ignore the case 2.
Hello thompson91,

Sometimes it helps to know what is going wrong and why before you work on fixing the problem.

Looking at your code:
1
2
3
4
5
6
7
8
9
10
11
12
bool operator< (const Date& compare)
{
    if (Year < compare.Year)
        return true;

    else if (Month < compare.Month)
        return true;
    else if (Day < compare.Day)
        return true;
    else
        return false;
}

The first if statement compares two years, but should this evaluate to "true" the next line returns from the function not having a chance to check the "else if" statements. Also as keskiverto ,mentioned each part has 3 possibilities, which you need to take into account.

One possible solution would be to create 3 bool variables and instead of returning from the if statements set a variable. You would also need 3 separate if statements and not the "else if" statements that you have. Hint: think about how you reach the "else if" statements and what happens when the first if statement is true. Then you can loose the last "else" statement in favor of return year && month && day;. If all are true it will return true otherwise it will return false.

Something to think about.

Andy
I am not saying this is the best solution, but a start at understanding what is going wrong.
I did miss lastchance's beautiful ninja:
1
2
3
      if ( Year  != compare.Year ) return Year  < compare.Year ;
      if ( Month != compare.Month) return Month < compare.Month;
      return Day < compare.Day;

What can we learn?

1. IF RETURN does not need ELSE
When you have if ( cond ) statementA; else statementB; only one fo the two statements is executed.
IF the cond is true, THEN the else is skipped entirely.
When you return from function, you do skip the rest too; the else does not make a difference.

2. The condition is Year != compare.Year. That is same as not (Year == compare.Year)

Essentially:
1
2
if ( Year  != compare.Year ) // the year decides
else // must look at month (and day?) 

Which could be written:
1
2
if ( Year  == compare.Year ) // must look at month (and day?)
else // the year decides 


Now we seem to have only two cases: either year is same, or it is not.
However, the "year is not same" contains exactly two cases:
* A is before B
* A is after B
Since A cannot be B when A!=B, we can get a solution with A<B

3. We return the value of expression: return Year < compare.Year ;
You could expand the if ( Year != compare.Year ) return Year < compare.Year ; into:
1
2
3
4
5
6
if ( Year  != compare.Year )
{
  bool smaller = Year  < compare.Year ;
  if ( smaller == true ) return true;
  else return false;
}

That, however, is awfully redundant. Too much to type without benefit. The more you type, the more errors can lurk in.
Registered users can post here. Sign in or register to post.