Dates and C++

This is a discussion on Dates and C++ within the C++ Programming forums, part of the General Programming Boards category; How do i write program in C++ to check if a date is before April 5,2008? I have this if ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    12

    Unhappy Dates and C++

    How do i write program in C++ to check if a date is before April 5,2008?

    I have this if statement:
    if(month<=4||day<5&&year<=2008)

    but it is not evauluting day correctly. What i am trying to do is see if a date (from a file) is before april 5,2008. I am reading month and day separately.

    Does anyone know how to code this?

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I would use the <ctime> functions and convert to a time_t and do a direct comparison.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,305
    This is what you want I believe:
    Code:
    if (year <  2008 ||
       (year == 2008 && month <  4) ||
       (year == 2008 && month == 4 && day < 5))
    The reason for the extra bits is that day < 5, for example, is meaningless on its own as the year or month could be anything.

    It can be shortened slightly by combining the two year == 2008 bits, but then you'd probably find it slightly harder to understand.
    Last edited by iMalc; 10-17-2008 at 01:32 PM.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    12
    umm im not sure what that means. Can you give me an example in this case.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    12
    oh, wow that would work
    thanks.

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    could it not be
    Code:
    if ( year <= 2008 && month <= 4 && day < 5 )
    ? Or maybe I am just having serious logic problems today.

  7. #7
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Logic problem day....

    if the year is less than 2008
    OR
    if the year is 2008 but the month is earlier than April
    Or
    if the year is 2008 and the month is April, bu the day is before the 5th.


    I.e. The problem with your code is that if the date were December 25, 1999. The statement would fail.

  8. #8
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Quote Originally Posted by Raigne View Post
    could it not be
    Code:
    if ( year <= 2008 && month <= 4 && day < 5 )
    ? Or maybe I am just having serious logic problems today.
    If the date passed is Nov 20, 2007, that will fail (on the month and day test)
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  9. #9
    Registered User
    Join Date
    Oct 2008
    Posts
    12
    anthor problem
    i need to check if something is more than 62 but less than 115. how would i code it.

  10. #10
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Well Todd's example wasn't quite as Christmassy as mine... Nor does he seem to want to party like its 1999......

  11. #11
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    if(x > 62 && x < 115)

  12. #12
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Why do so many C programmers and Java programmers not put a space between the "if" and the "("?

    Todd
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  13. #13
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Java was the first real language I learned.... and C would be numero dos... So maybe that is why for me. I don't now... I just don't like putting a space there.

    Hohoho

  14. #14
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    haha, i see my stupidity...

  15. #15
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Quote Originally Posted by Raigne View Post
    haha, i see my stupidity...
    I am glad you have found yours. I am still endlessly trying to find mine... much to no avail.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21