So I'm trying to validate dates and I have this else if "bool" that always returns false. I've written this at least 10 times and can't get it to work. When I comment everything out and only try one if at a time they all work. I know it's semantics, but I can't figure it out.

if ((month >= 1 && month <= 12) || (day >=1 && day<= 31) || (year >= 1753 ))
       return false;
    else if((month == 12) && (day == 31) && (year == 9999))
      return false;
     else if ((!isLeapyear(year)) && (day > 28))
        return false;
    else if (month > 12)
       return false;
    else
   return true;

Recommended Answers

All 7 Replies

You're returning false if the month is between 1 and 12 or the day is between 1 and 31 or the year 1753 or later. So if you want to get true, you have to give a day that's less than 1 or greater than 31 (and if it is greater than 31, it also needs to be a leap year), a month that's less than 1 (if it's greater than 12, line 7 will take effect) and a year that's less than 1753.

I do this

if ((month >= 1 && month <= 12) || (day >=1 && day<= 31) || (year >= 1753 ))
       return false;

it works

this

if((month == 12) && (day == 31) && (year == 9999))
      return false;

works

this

if ((!isLeapyear(year)) && (day > 28))
        return false;

works

this

if (month > 12)
       return false;

works

I can get each if stament to work if the other if statement is commented out.

This always returns false also.

if ((month >= 1 && month <= 12) || (day >=1 && day<= 31) || (year >= 1753 ))
       return false;

    if((month == 12) && (day == 31) && (year == 9999))
      return false;

     if ((!isLeapyear(year)) && (day > 28))
        return false;

    if (month > 12)
       return false;

   return true;

If you enter 0 0 0 as the input of your code, the loop terminates. So as I said, the function will return true for that input.

It would be better if you told us what your input is and what you're expecting this function to do.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.