Results 1 to 5 of 5

Thread: calculating someones age

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2000
    Posts
    55

    Question calculating someones age

    I am trying to calculate the age of someone. I am having the user input his age as a string, but I was just doing year they were born. However i need it to calculate the date to ensure that it is before or after there birthday to get the correct age of the user. How do i compare to dates to find the users age? I am at a stand still on this, any help appreciated.
    Beres

  2. #2
    Hyperactive Member CaptainPinko's Avatar
    Join Date
    Jan 2001
    Location
    London, Ontario, Canada
    Posts
    332
    well you could ask them to enter their month in numeric form (ie. jan = 1, feb = 2, mar = 3... etc)

    Code:
    // "u" is for user, "t" is for today's
    int u_year, u_mouth, u_day;
    int t_year, t_month, t_day;
    
    if ((u_month <= t_month) && (u_day <= t_day))
    // already had their birthday
                     {return (u_today - t_day);}
    else
    // haven't had their birthday
                     {return (u_today - t_day - 1);}
    "There are only two things that are infinite. The universe and human stupidity... and the universe I'm not sure about." - Einstein

    If you are programming in Java use www.NetBeans.org

  3. #3
    Hyperactive Member
    Join Date
    May 2000
    Posts
    367
    Hey Cap'n

    not sure if your if works

    it requires the month and day always to be less than.

    Shouldn't it be

    Code:
    if(u_month < t_month || (u_month==t_month && u_day<=t_day))
    {
    // already had their birthday
    
    }
    else
    {
    // haven't had their birthday
    
    }

  4. #4
    Hyperactive Member CaptainPinko's Avatar
    Join Date
    Jan 2001
    Location
    London, Ontario, Canada
    Posts
    332
    you're right, my bad
    "There are only two things that are infinite. The universe and human stupidity... and the universe I'm not sure about." - Einstein

    If you are programming in Java use www.NetBeans.org

  5. #5
    Hyperactive Member
    Join Date
    May 2000
    Posts
    367
    no prob Cap'n.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width