How would you go about creating an exact age calculator

Pages: 1234
dhayden wrote:
Like I said, this is harder than it appears.


It isn't really. You just have to map out the logic for it. This is less of a programming challenge and more of a logic challenge -- but the logic is something OP should know how to do. His hangup is probably that he's trying to write the code before he figured out what he wants his code to actually do.


Let's use your example:

cur date: 2014/8/7
birth date: 2013/8/15

What is the age? Before even thinking about how you'd code it... do the problem by hand.


I did it by hand and came up with:
0 years, 11 months, 23 days


When you think about it... it's just a problem of subtraction. Subtract cur_date - birth_date. Start with days, and work down towards the 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



You just have to work out the logic.
Case A:
(2013-03-01) - (2012-02-29) = ?

Case B:
(2014-08-31) - (2014-08-01) = ?
((2014-08-31) + 31) - ((2014-08-01) + 31) = (2014-10-01) - (2013-09-01) = ?
I'm assuming helios is pointing out edge cases that don't quite work with above logic.

Case A:
(2013-03-01) - (2012-02-29) = ?


Oho. I guess you don't take the number of days from the previous month, but rather take them from the birth month. Good catch

In that case:

1 - 29 = -28 + 29 (borrow from birth month) = 1 day

3 - 2 - 1 (borrowed) = 0 months

2013 - 2012 = 1 year


EDIT: ah but that's still wrong because it would just be a year exactly. So I was right the first time!

Case B:


Don't see what's so special about this one. It doesn't require any borrow, so it's probably the simplest example yet.
Last edited on
(x + z) - (y + z) should equal x - y. Finding an algorithm with this property is really difficult. You might ask "why is this important?" Without that property, "1 month" is a meaningless value.
I'm not following you at all. "1 month" has meaning in that we are counting the number of months.

It sounds like you are over-complicating a simple problem to me. OP needs to find the number of years,months,days between two given dates. The above logic accomplishes that relatively simply, even if it doesn't strictly abide by ?transitive? laws of mathematics.

It loses meaning if you try to compare the ages of two people.
The [COleDateTimeSpan] also does not model in terms of years and months but only days, hours, minutes and seconds.


Keep in mind not all months are 31 days and there are leap years every 4 years except every 100 years excluding every 400 years.

January, March, May, July, August, October, and December = 31 days
April, June, September, and November = 30 days
February = 28 (or 29 on leap year) days

Should be a fairly trivial task to accomplish.
Alright guys, first of all thanks for the feedback you've been giving me, amazing. So im really gonna let the code speak for itself since it would be pretty easy to see what i want. Apparently my code doesn't work.
1
2
3
4
5
6
7
{
    if ( (cDay > 29) || (cMonth == 2))
        cout << "The program will shutdown, next time type in a valid number";
        return 0;


}
if ( (cDay > 29) || (cMonth == 2))


|| means that if either of those conditions are true

So you are saying the number is invalid if the day is > 29 OR the month is 2. This means that any date in February will be invalid... and any date above 29 in any month will also be invalid.
Is there anyway to say if it's the second month and (in the second month) the day is above 29, then cout "blabla??"
if it's the second month and (in the second month) the day is above 29


Key word emphasized.

Use the && operator to do an 'and' operation.
Alright so what if i want to make a double and? When i type in the cMonth/ Current Month it crashes, it doesn't matter which month it crashes with all of them.
1
2
3
4
5
 if ( (cYear > 2014,2015,2017,2018,2019,2021,2022,2023,2025,2026,2027,2029,2030) && (cDay >= 29) && (cMonth = 2))
{
    cout << "The program will shutdown, next time type in a valid number";
return 0;
}
Last edited on
Alright so what if i want to make a double and?


Then put in two &&s =P

1
2
3
4
if( foo && bar && baz )
{
  // this will execute only if foo,bar, and baz are all true
}


As for your code:

 
(cYear > 2014,2015,2017,2018,2019,2021,2022,2023,2025,2026,2027,2029,2030)


This does not do what you think. And in fact, I don't even know what you're trying to do here. General rule of thumb is: don't use the comma operator. The comma operator does not do what you think it does.

If you want to check for two different years:

1
2
if(cYear == 2014,2015) // <- wrong
if((cYear == 2014) || (cYear == 2015)) // <- right 


Although checking each individual year is a terrible idea. You can check for leap years by checking to see if the year is divisible by 4 (hint: use the % operator)



Also:

cMonth = 2

= is assignment.
== is comparison.
Although checking each individual year is a terrible idea. You can check for leap years by checking to see if the year is divisible by 4 (hint: use the % operator)
Can you give an example? Im lost. So the formula is that if it's a leap year, then the current year divided by 4 will give an integer?
1
2
3
4
5
if(year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) //leap year
//or if(!(year % 4) && (year % 100 && !(year % 400)))
//every 4 years is leap year
//except every 100th year
//excluding every 400th year 


*had flaw in logic mentioned by helios
Last edited on
2000 was a leap year.
Giblit can you explain to me how i would include the other parts. Like if it a leap year then you can type in 29 but otherwise you can't type 29 or above
if((leapyear && day > 29 && February) || (!leapyear && day > 28 && February))
Alright, im having trouble actually understanding what it means, could someone translate into words like: if x and y is z or a and b is c. I haven't quite figured what the ||, &&, and ! means i know it's logical NOT but what does that mean?
Pages: 1234