How would you go about creating an exact age calculator

Pages: 1234
That is when they were introduced. All a leap year does is make it so that the year is closer to what it should be (the time it takes to rotate around the sun). Without leap year our years would be 365 days instead of ~365.24 so that is why there are 97 leap years every 400 years to compensate 97/400 = .2425.


What I meant though was that they are probably going to be checking relatively modern dates. Since it is for an age calculator. Though they could be seeing how much older Julius Caesar is than them.
1. Assumes that std::time_t is Unix time (almost universally true)
http://en.wikipedia.org/wiki/Unix_time
2. Does not account for leap seconds http://en.wikipedia.org/wiki/Leap_seconds
3. Brute force

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <iostream>
#include <ctime>
#include <stdexcept>
#include <utility>
#include <tuple>

std::tm& sanitize( std::tm& tm )
{
    if( std::mktime( std::addressof(tm) ) == -1 )
        throw std::invalid_argument("invalid date") ;
    return tm ;
}

// increment year to start of unix epoch; return number of years added
int increment_to_unix_epoch( std::tm& tm )
{
    static constexpr int begin = 70 ;
    int n = 0 ;
    while( tm.tm_year < begin ) { ++tm.tm_year ; ++n ; }
    sanitize(tm) ;
    return n ;
}

enum month_t : int { JAN = 0, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC } ;

std::tm construct( int year, month_t month, int day, int bhour, int bmin, int bsec )
{
    std::tm tm {} ;
    tm.tm_mday = day ;
    tm.tm_mon = month ;
    tm.tm_year = year - 1900 ;
    tm.tm_hour = bhour ;
    tm.tm_min = bmin ;
    tm.tm_sec = bsec ;
    tm.tm_isdst = -1 ; // don't know
    return tm ;
}

// return #increments added to n
int increment( std::tm& from, std::time_t till, int std::tm::*member, int n = 0 )
{
    sanitize(from) ;
    while( std::mktime(&from) < till ) { ++(from.*member) ; ++n ; }
    if( std::mktime(&from) > till ) { --(from.*member) ; --n ; }
    return n ;
}

int elapsed_years( std::tm& from, std::time_t till )
{
    int n = increment_to_unix_epoch(from) ;
    return increment( from, till, &std::tm::tm_year, n ) ;
}

int elapsed_months( std::tm& from, std::time_t till )
{ return increment( from, till, &std::tm::tm_mon ) ; }

int elapsed_days( std::tm& from, std::time_t till )
{ return increment( from, till, &std::tm::tm_mday ) ; }

int elapsed_hours( std::tm& from, std::time_t till )
{ return increment( from, till, &std::tm::tm_hour ) ; }

int elapsed_minutes( std::tm& from, std::time_t till )
{ return increment( from, till, &std::tm::tm_min ) ; }

int elapsed_seconds( std::tm& from, std::time_t till )
{ return increment( from, till, &std::tm::tm_sec ) ; }

int main()
{
    int byear = 1922 ; // std::cin >> byear ;
    int bmonth = NOV ; // std::cin >> bmonth ;
    int bday = 30 ; // std::cin >> bday ;
    int bhour = 14 ; // std::cin >> bhour ;
    int bmin = 38 ; // std::cin >> bmin ;
    int bsec = 24 ; // std::cin >> bsec ;

    std::tm birth_date = construct( byear, month_t(bmonth), bday, bhour, bmin, bsec ) ;
    const std::time_t now = std::time(nullptr) ;
    
    // *** attention *** to be called in order: most significant to least significant
    std::cout << elapsed_years( birth_date, now ) << " years, " ;
    std::cout << elapsed_months( birth_date, now ) << " months, " ;
    std::cout << elapsed_days( birth_date, now ) << " days, " ;
    std::cout << elapsed_hours( birth_date, now ) << " hours, " ;
    std::cout << elapsed_minutes( birth_date, now ) << " minutes and " ;
    std::cout << elapsed_seconds( birth_date, now ) << " seconds\n" ;
}

http://rextester.com/UNXVF84530
So this was the original PM i sent to Disch but i figured i might as well post it here for a fast response:

Alright so in this thread http://www.cplusplus.com/forum/general/139813/2/ you said the following about calculating days months years.

days
cur = 7
birth = 15

7 - 15 = -8 (can't have negative days, so borrow 31 days from the previous month)
-8 + 31 = 23 days

months
cur = 8-1 = 7 (borrowed 1 to add 31 days)
birth = 8

7 - 8 = -1 (can't have negative months, so borrow 12 months from the previous year)
-1 + 12 = 11 months

years
cur = 2014 - 1 = 2013 (borrowed 1 to add 12 months)
birth = 2013

2013 - 2013 = 0 years





So im gonna post the part of my code which does these calculations, remember im a huge newbeginner to C++ so you're allowed to facepalm if you see something dumb.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
int Alder = cYear - bYear; // His age
  


  if (bMonth > cMonth) 
  {
      cout << "You are ";
cout << Alder - 1 ; // It isn't the persons birthday yet
cout << " years old";

  }
  else
  {
      cout << "You are "; // It has been the persons birthday
      cout << Alder;
      cout << " years old";


  }





// Days

int ferdigDage = cDay - bDay;



if (cDay > bDay) // If the current day has a higher value than the day of birth then just subtract without borrowing

{
    cout << " and ";
    cout << ferdigDage;
    cout << " days old";

}
else // In case his birth day is greater than current day then borrow 31 from the previous month (July)
{
    cout << " and ";
    cout << ferdigDage + 31;
    cout << " days old";
}

// Months
int dManeder = cMonth - 1 - bMonth + 12; // This is only if we borrowed the 31 days

int bManeder = cMonth - bMonth; // If this is not the case then just do a simple subtraction

if (bDay > cDay) // If we borrowed then do this calculation cMonth -1 - bMonth + 12 ( we can't have negative numbers)

{
  cout << " and";
        cout << bManeder;
        cout << " months old";
}
else // If we haven't borrowed anything then just subtract the current month with the birth month
{
        cout << " and ";
    cout << dManeder;
    cout << " months old";
}




So i tried typing in my brothers date since this was the first one that came to mind, since his birthday is in September. So his birthday is 3/9/1999. So this is exactly what happend.


Type in the current year: 2014
Type in the current month: 8
Type in the current day: 30
Type in the birth year: 1999
Type in the birth month: 9
Type in the birth day: 3

You are 14 years, 27 days and 10 months old
Last edited on
Still need help.
Topic archived. No new replies allowed.
Pages: 1234