Thread: date comparing

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    4

    date comparing

    Hi,

    Input: Names and born dates of persons
    Output: Oldest person and his/here born date

    How to do this? I'm new at this so be gentle

    thx

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    a) If A is born in 1940 and B in 1941 then A is older.
    else if years are the same
    b) If A is born in September and B in October then A is older.
    else if months are the same
    c) If A is born on the 13th and B on the 14th then A is older.
    else they are both the same age

    Hope this gives you an idea.

    But you really should try to formulate some ideas, e.g how you are going to represent data, how you are going to search the persons etc, post some code and ask more specific questions.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    4
    thx for fast replay

    I need to use array if I have more than two names.

    I know how to make this in delphi, so I need someone
    who can explain me how arrays, dates, etc works in C.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    If you only want to display data for one person you don't need arrays. Just store the data for the currently oldest person and discard younger ones.

    For dates may-be you can use something from the <time.h> header, but the data for a person might be as simple as a struct containing a string (name) and three integers (year, month, day).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    time_t holds dates from 0 to max. 0 I think starts at January 1, 1970. So if you convert the dates into time_t then comparing dates is a simple unsigned long (time_t) comparison.

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    4
    I'm trying but something isn't right...
    Code:
    #include <stdio.h>
    #include <time.h>
    int main()
    {
    	double dif;
    	time_t t1, t2;  
            dif=difftime(t1,t2));    	
    	clrscr();
    	printf("First date:");
    	scanf("&#37;d",t1);
    	printf("Second date:");
    	scanf("%d",t2);	
     	printf("\nThe difference between dates is: ", diff);
    }
    what do I need here?

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Well, first of all, your call to difftime is done BEFORE you read the time variables from the user, so that won't work.

    Also, it won't compile, because you have one variable "dif" that is declared, then printing a "diff" which is undeclared, so I guess you have to fix that.

    Just to clarify, if you use "Microsoft Excel", you can declare a calculation, and then change the variables that the calculation relates to afterwards, and get the new value out of it. C, C++ and most other programming languages, the calculation is done with the values the variable have at the time, and no matter what other changes you do later, the value stays the same. So you need to make sure the variables are "filled in" before you use them for a calculation.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Nor can you use &#37;d to scan directly into a time_t.

    First you need to use fgets() to read a line.
    Then use strptime to parse the line and fill in a tm struct with the parsed information. If you don't have strptime, then you need to roll your own to do the same thing.
    Then you use mktime() to turn your tm struct into a time_t.
    Finally, having done that twice, you have two time_t variables which you can difftime()
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by matsp View Post
    Just to clarify, if you use "Microsoft Excel", you can declare a calculation, and then change the variables that the calculation relates to afterwards, and get the new value out of it. C, C++ and most other programming languages, the calculation is done with the values the variable have at the time, and no matter what other changes you do later, the value stays the same. So you need to make sure the variables are "filled in" before you use them for a calculation.
    Ah so that's what's going on in the minds of a few posters recently.
    I was baffled as to what confusion of ideas made people think that they could do the calculations before they have read in the data! A quantum computer with reversal of cause and effect perhaps?!?!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by iMalc View Post
    Ah so that's what's going on in the minds of a few posters recently.
    I was baffled as to what confusion of ideas made people think that they could do the calculations before they have read in the data! A quantum computer with reversal of cause and effect perhaps?!?!
    Of course I have no idea what the people writing such code are actually thinking - but the Excel example has crossed my mind before.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Registered User
    Join Date
    Jan 2008
    Posts
    4
    Ok. Maybe I posted wrong code... I have tried diff and dif (typing mistake).
    Also, I declare diff=difftime(... after input, also didn't work. I will try SALEM's
    post. I think it would we correct... thx...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Advancing day by day until it matches a second date
    By nhubred in forum C++ Programming
    Replies: 1
    Last Post: 05-30-2009, 08:55 AM
  2. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  3. Date program starts DOS's date
    By jrahhali in forum C++ Programming
    Replies: 1
    Last Post: 11-24-2003, 05:23 PM
  4. CDate Class - handle date manipulation simply
    By LuckY in forum C++ Programming
    Replies: 5
    Last Post: 07-16-2003, 08:35 AM