I am trying to create a program that finds out how many months they have been alive, but have been running into some issues. Here is my function so far:
int getResult(int year, month, day, endResult)
{
int thisYear, thisMonth, thisDay;
year = thisYear - year;
year *= 12;
}
And what I’m trying to accomplish would show an output like:
Output:
What year were you born?
1989
What month were you born?
5
What day were you born?
23
You are x months old.
I was going to continue with months but then I realized, what if the month they were born is in after this month or before? So, if anyone has any tips on how to calculate that, I’d appreciate it.
Let’s see. First, let’s say now is:
and your birthday is:
Now, we go case by case:
month_now == month_birth: as you have already computed:month_now > month_birth: easily, you have:month_now < month_birth: in this case,(year_now-year_birth)*12gives you more months then necessary, and you have to subtract:Now if you look carefully, you will see that they are all in fact the same formula:
(in the third case,
month_now-month_birthis negative)