-
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.
-
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);}
-
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
}
-
-