1. ## Counting days

Have a question here, I am working on a program that will calculate how old a person is using years, days and months.

How would I calculate how many days old someone is from their birthday to the present day? Same with months? I got the years easy

***EDIT*** Doh dont need the #include string..

Here is what I got so far:

Code:
```#include <iostream>
#include <string>
using namespace std;

int main ()
{

int yob=0;
int dow=0;
int mob=0;
int result=0;
int currentyear=2003;
int currentday=16;
int x=0;

cout<<"Enter your Year of birth: ";'\n';
cin>>yob;

result= yob - currentyear;

cout<<"You are"<<result<<"years old"<<'\n';

cout<<"Enter you Day of Birth: ";'\n';
cin>>dow;

while(dow != 365){
x=dow+365;

result = dow - currentday;

cout<<"You are"<<result<<"days old"<<'\n';

return 0;

}```
Anyone help?

2. If you want it to be precise (including extended years: 366), you could do it simply by looping from the year of birth (added with the days past in that year) to the current year and day. The following code might give a better understanding.

Code:
```DaysOld = ...days past in birth year...

for (Year = BirthYear; Year <= CurrentYear; Year++)

DaysOld += DaysPerYear;
if ( ...extended year... )  DaysOld++;
}
DaysOld += ...days past in current year...```
The problem is that you have to calculate if the looping year is extended or not calculate how many days have past in both the birth year and current year.
Note that you could also do this without an loop if could calculate the number of extended years between the birth and current year. But I wrote this code to give you an better understanding.

3. This is an example of how important algorhithms are. Once you can do this on paper with a pen or pencil, writing the code is easier.

One way to do it is to say that you will count full years, full months and determine any left over days. A year would be defined as twelve consecutive months. Start by taking the date of birth, say 7/16/1983 and current date, say 7/16/2003, to make things simple. compare months. they are the same, so compare days. they are the same, so just subtract years to get an age of 20 years, 0 months, and 0 days. Now try to generalize this by changing the days, months, or years individually to see what you have to do different to get the correct answer. For example, try DOB being 4/15/86. Birthday month in 2003 is before current month so you have 17 full years plus 2 full months (May and June). But you also have 2 partial months, so how do you want to handle them? One approach is to just add them up and divide by 30, or 31, or 28----your choice. Another is to go by the day. Using that approach there are three full months and 1 extra day. Now try DOB 11/26/70. Birthday month is after current month so last full year was 2002. Then there are from 11/26/70 there have been 8 months (ending June 26) and 4 days in June plus 16 days in July for total of 20 days. So the general pattern seems to be: find the last full year, if today is not birthday find the last full month, determine left over days. Details of finding the last full year are take current month and month of birth. If current month larger than month of birth, use current year. Else use current year minus one. Subtract current year from year of birth to get full years since birth. Details of finding the last full month include take current day and compare to day of birth. if current day is larger than day of birth, then count months. To count months, if current year same as last full year, just subtract months. Otherwise, subtract birth month from 12 and add result to current month. To determine left over days, determine how many days in prior month (if there is one), subtract day of birth from number of days in that month and add it to current date. If current month same as birth month, just subtract the days. Now try to confirm this sequence by using some new dates. If you can confirm it, then you can see the potential for using several functions such as calcDifference() within which are calculateFullYears(), calculateFullMonths(), and calculateDays(). Within each function are the details of how to do the calculations, using the above description in English of what to do.

The alternative is to calculate how many seconds have passed between the two dates and determine how many days that represents. Then, taking into account leap years (or not at your discretion), determine how many full years there are, how many full months there are, and how many full days there are. difftime() in ctime (or time.h) is the function that can give you the difference, if I remember correctly.

4. I know you checked out my CDate class. It can do just what you need. For example:
Code:
```CDate today;
CDate birthday("19750101");
int days = today - birthday;```
If you are particularly interested in calculating it yourself, you can look at the source of my class to help you figure out how to get it done.

5. ## still stuck

Still stuck on this, tried inputting your code letter for letter and I keep getting errors.

Could you post a full example?

6. Really? What errors?

Here's a full program:
Code:
```#include <iostream>
#include "CDate.h"
using namespace std;

int main() {
CDate today;
CDate birthdate(7, 17, 1975);

cout << "There are " << (today - birthdate) << " days between " <<
birthdate.Format("MMM/DD/YY") << " and " << today.Format("MMM/dd/yy") << endl;

return 0;
}```