How to calc. your age in days..?

This is a discussion on How to calc. your age in days..? within the C Programming forums, part of the General Programming Boards category; Does anybody have a good idea, to how the easiest way to calc you age in days. ------------------------------------code until now. ...

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    64

    Question How to calc. your age in days..?

    Does anybody have a good idea, to how the easiest way to
    calc you age in days.

    ------------------------------------code until now.

    int yearcalc_how_many_y, yearcalc_how_many_days;
    int date, month, birthyear, age;

    int calc_age_i_dage(int date, int month, int birthyear)
    {
    int currentyear = 2002;
    printf("plz key you birthday<dd>: ");
    scanf("%d", &date);
    printf("Plz key in month <mm>: ");
    scanf("%d", &month);
    printf("plz key in birthyear <yyyy>: ");
    scanf("%d", &birthyear);
    age = date+month+birthyear;

    yearcalc_how_many_y = (currentyear - birthyear);
    printf("%d",yearcalc_how_many_y);
    yearcalc_how_many_days = (yearcalc_how_many_y * 365)+((yearcalc_how_many_y/4)-1);
    printf("%d",yearcalc_how_many_days);

    return age;
    }


    int main()
    {

    struct date d;

    getdate(&d);
    printf("The current year is: %d\n", d.da_year);
    printf("The current day is: %d\n", d.da_day);
    printf("The current month is: %d\n", d.da_mon);

    calc_age_i_dage(date, month, birthyear);

    printf("Age: %d", age);

    getch();
    return 0;
    }

    /*
    Which year is leap year.
    <2000>-1996-1992-1988-1984-1980-1976-1972-1968-1964-1960-1956-1952-1948

    Year 2000 didn't count, because, year which divides with 400 is not a leap year
    */
    !G!

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    What kind of precision do you need? If it doesn't matter then just multiply the age in years by the number of days in a standard year.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    imho, your program's flawed. scrap it, and think up a new one. try to limit your variables as much as possible

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You can simplify this, as Prelude stated:

    days_alive = (int)(365.25 * years_alive);

    Actually, you could also use standard 'time' functions and do a bit of subtraction also...

    Quzah.
    Last edited by quzah; 03-11-2002 at 09:12 PM.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    64
    yes prob, but is that precis enough, it's has to be 99% correct..
    !G!

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > yes prob, but is that precis enough, it's has to be 99% correct..

    If it only hast to be 99% correct, you don't even have to bother
    using .25 on the end. Hell, you can bee over 3 days off PER YEAR
    in your end result.

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >yes prob, but is that precis enough, it's has to be 99% correct..
    99% correct in this context is way off and gives you a lot of breathing room.
    Calculate the current number of days that have passed in the current year then just
    totalDays = ( totalYears - 1 ) * 365 + currentDays;
    and you'll still be very accurate without having to bother with leap year and all of that junk.

    The hardest part will be counting the days that have passed in the current year, which should tell you how easy this can be.

    -Prelude
    My best code is written with the delete key.

  8. #8
    Normal vector Carlos's Avatar
    Join Date
    Sep 2001
    Location
    Budapest
    Posts
    463
    My opinion is: if you do something, then do it right.
    If the algorythm you're using is not 100% accurate, well, don't call it algorythm.

    This might be helpful:http://www.codeproject.com/datetime/

    Btw, a quick serach on the net for "date time routines" or something like that will help you out for sure, I just don't have the time right now.

    Have a nice code! ;)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Advancing Days
    By nhubred in forum C++ Programming
    Replies: 0
    Last Post: 06-01-2009, 06:22 PM
  2. Calc age use Date of PC
    By miziri in forum C++ Programming
    Replies: 12
    Last Post: 03-18-2006, 04:01 AM
  3. wow...technology today...
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 29
    Last Post: 02-01-2003, 06:37 PM
  4. Age in Minutes
    By harryP in forum C++ Programming
    Replies: 7
    Last Post: 09-09-2002, 10:40 PM
  5. while, sentinel, if, switch
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 11-11-2001, 10:50 PM

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