0

I have a custom data structure date in C++11:

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

I want to compare two dates and write function for it:

int compare_dates(date a, date b) {
    int result = 0;

    if (a.year < b.year) {
        result = -1;
    } else if (a.year == b.year) {
        if (a.month < b.month) {
            result = -1;
        } else if (a.month == b.month) {
            if (a.day < a.day) {
                result = -1;
            } else if (a.day > a.day) {
                result = 1;
            }
        } else {
          result = 1;
        }
    } else {
        result = 1;
    }

    return result;
}

But this function doesn't work correctly. I spent a lot of time to debug it and found some issue in the following part of code:

} else if (a.month == b.month) {
    if (a.day < a.day) {
        result = -1;
    } else if (a.day > a.day) {
        result = 1;
    }
} else {
  result = 1;
}

There are two screenshots during debugging, first on } else if (a.month == b.month) { and second when I click next line in debugger. Such happens for all inputs. Why debbuger did not enter into if (a.day < a.day) { or result = 1; everytime?

Narek Atayan
  • 1,479
  • 13
  • 27
  • You may have undefined behaviour elsewhere, which can lead to all kinds of strange things. The error cannot be reproduced by the portions you have shown us. Post a complete piece of code, please. See http://stackoverflow.com/help/mcve – Christian Hackl Nov 19 '16 at 10:52
  • Thank you @ChristianHackl for the comment, next time I will add code according to *mcve* standard. – Dmytro Rudnitskikh Nov 19 '16 at 11:06

3 Answers3

2

Try with

if (a.day < b.day) {
    result = -1;
} else if (a.day > b.day) {
    result = 1;
}

instead of

if (a.day < a.day) {
    result = -1;
} else if (a.day > a.day) {
    result = 1;
}

The two tests a.day < a.day and a.day > a.day are ever false, so result = -1 and result = 1 are never executed.

I suppose that the compiler optimize the code as follows

} else if (a.month == b.month) {
} else {
  result = 1;
}
max66
  • 65,235
  • 10
  • 71
  • 111
1

It may be that compiler optimisations get in your way.

As others have observed, you have two typos inside the block. a.day < a.day and a.day > a.day are by definition always false. Consequently, nothing can ever happen in the block. There is no observable behaviour. That's probably why the compiler just completely eliminates the entire thing to save speed and/or space.

I don't know which compiler you are using, or which flags you pass it, but if you disable all optimisation, then the debugger should work as you expect. You can also try to put a std::cout << "...\n"; into the block to enforce some observable behaviour and see if that changes something.

This is also an excellent reason to actually learn how your compiler really works, without all the IDE fluff covering its functionality.

Christian Hackl
  • 27,051
  • 3
  • 32
  • 62
0

Line no 25 in screenshot first,

if (a.day < a.day)

It should be

if (a.day < b.day)

same mistake is in line 27.

Rahul
  • 262
  • 1
  • 5
  • 22