Thread: Checking for leapyear

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    56

    Checking for leapyear

    i have this from my book, but is there a better way to do it?
    Code:
     
    
    int Date::checkDay (int TestDay) const
    {
    static const int daysPerMonth [ 13 ] =
        { 0, 32, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
    
    if ( testDay > 0 && testDay <= DaysPerMonth[ month ] )
    return testDay;
    
    //February 29 check for leap yr
    if ( month == 2 && testDay == 29 && 
     (year & 400 == 0 ||
      (year % 4 == 0||
       (year %4 == 0 && year % 100 !=0)  )  )
         return testDay;
    return 1; //leave object consistent, even if bad value
    }
    Last edited by criticalerror; 03-03-2004 at 07:25 PM.

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Our calendar system is rather screwy, so it is unlikely that you'll find something significantly more elegant. The one thing I have a problem with in that code is the "return 1;" at the end (along with its comment). If all is not fine, the function (written as is) should porbably through an exception, so that there is some notification that your object is in a bad state.

  3. #3
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    Code:
    { 0, 32 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
    You are missing a comma, I hope that this wasn't pasted directly from the cd that came with your book.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    It's also wrong.

  5. #5
    Registered User
    Join Date
    Dec 2003
    Posts
    56
    Originally posted by neandrake
    Code:
    { 0, 32 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
    You are missing a comma, I hope that this wasn't pasted directly from the cd that came with your book.
    no.. i hand typed it in... my bad... changing it in the post.

  6. #6
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    Originally posted by swoopy
    It's also wrong.
    what part is wrong? if you are referring to zero being the first number, he probably doesn't want daysInMonth[0] to be the first so he can start at 1.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Originally posted by neandrake
    what part is wrong? if you are referring to zero being the first number, he probably doesn't want daysInMonth[0] to be the first so he can start at 1.
    No, not that.

  8. #8
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    I thought I saw some problems too. Can't you just input the year and if (year % 4) == 0 then it's a leap year?
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Originally posted by neandrake
    I thought I saw some problems too. Can't you just input the year and if (year % 4) == 0 then it's a leap year?
    Yes, at least the way criticalerror typed in the formula.
    (year % 4 == 0||
    I'm guessing that line shouldn't even be there.

    Plus there's this:
    (year & 400 == 0 ||
    Perhaps just a typo. And the if()'s missing some parentheses.

  10. #10
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317

    Are there 32 days in Jan?

    Is there some reason the 2nd number is 32?
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  11. #11
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640

    Re: Are there 32 days in Jan?

    Originally posted by manofsteel972
    Is there some reason the 2nd number is 32?
    You're assuming he's using the roman calander. nah, i'm just stupid, i saw it but didn't think anything of it. that's why i'm a programmer.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  12. #12
    Registered User
    Join Date
    Dec 2003
    Posts
    56
    Alright, I am re posting what is supposed to be the correct code, I totally messed up before: this is what the books says:
    Code:
     
    int Date::CheckDay( int testDay ) const{
        static const int daysPerMonth [ 13] =
          { 0, 31, 28, 31, 30, 31, 31, 30, 31, 30, 31 };
    
        //determine if test day is valid for specified month
         if ( testDay > 0 && testDay <= daysPerMonth [ month ] )
               return testDay;
         //February 29 check for ly
        if ( month == 2 && testDay == 29 &&
           (year %400 == 0 ||
           (year % 4 == 0 &&  year % 100 !=0) ) )
                return testDay;
    cout << "Day" << testDay  << "invalid. Set to day 1.\n";
    
    return 1; //leave object in consistent state if bad value

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array bounds checking
    By rohan_ak1 in forum C Programming
    Replies: 2
    Last Post: 08-26-2008, 10:16 PM
  2. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  3. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 05:59 PM
  4. Forced moves trouble!!
    By Zishaan in forum Game Programming
    Replies: 0
    Last Post: 03-27-2007, 06:57 PM
  5. Problems about gcc installation
    By kevin_cat in forum Linux Programming
    Replies: 4
    Last Post: 08-09-2005, 09:05 AM