1. Algorithm help

I need to figure out how to determine from an input date the number of days to a set date. For example:

Code:
```     int Month;
int Day;
int Year;

cout << "Enter a starting date <mm dd yyyy>" << endl;
cin >> Month >> Day >> Year;

...```
I have the code written to validate a good date, account for leap year, etc.... I just do not know how to mathmatically work through the rest of the problem.

I DO NOT want code, I want to do the code myself...I do however need help working through the logic of the problem.

Thank you for your help and understanding.

2. >I just do not know how to mathmatically work through the rest of the problem
How are you going about it right now? A quick and easy (relatively) way is to convert your dates to the number of days in the year and then use simple arithmetic to find a difference.

3. I was thinking something along those lines, but am not sure how to go about that. For example if I were to enter 4 4 2004, how many days is it until 12/25/2005? How would I convert 4 4 2004 to an integer? What if the date were 1 1 1980?

This is getting my goat right now.....

4. >How would I convert 4 4 2004 to an integer?
Take the total number of days up to April and then add 4. That's how many days have elapsed this year.

>how many days is it until 12/25/2005
Find the number of days from January 1st 2005 to December 25th (no problem I hope ) and then add the difference from the total number of days in 2004 and the days that have already passed.

>What if the date were 1 1 1980?
What if it were? There's no difference except you have several full years in the middle to consider. The only crux part is leap years.

5. The time library of C++ just picked an arbitrary date years ago as its starting point. That might be acceptable for you if this program is working with birthdays or appointments.

6. What the project is supposed to accomplish is to input a date and have the program return the number of days until Christmas 2005.

The accepted starting date is 1/1/1980. This forces the consideration of leap years.

If I can figure out the logic to make this happen, I feel very confortable that I can come up with the code. My brain is just not working out the logic for some reason.

Thanks to everyone who have responded....

Mark

7. one way is to use the ctime (or time.h) file to help you. I forget the exact name of the function but in ctime is a function called t_diff (or something like that) that calculates the difference from current date to start of specific starting date in seconds, or milliseconds or something like that. You can use this function to calculate two differences, one for currentDate and one for Christmas 2005. Then you can calculate the difference in length between the two differences to get the time in seconds (or milliseconds) between the two dates. Then use the number of seconds (or milliseconds) in one day to determine the number of days between the two dates.

If using ctime isn't allowed in the exercise, then you can do a similar thing yourself. Alternatively, doing it yourself, it might be easier to determine the number of full years between the two desired dates. Determine how many of those years are leap years and compensate by one day for each leap day included. Then determine which full months separate the two dates, and add the number of days for each full month. Then determine the number of days in the two partial months. Then add em all up. For example:

start:
Jan 19, 2003

stop:
Dec 25, 2005

full years between Jan 19 2003 and Dec 25, 2003 is one---2004---= 365 days

number of leap years in between 1 = 1 day

number of full months in between:
feb 2005 = 28 days
march 2005 = 31 days
april 2005 = 30 days
may 2005 = 31 days
june 2005 = 30 days
july 2005 = 31 days
aug 2005 = 31 days
sept 2005 = 30 days
oct 2005 = 31 days
nov 2005 = 30 days

number of partial days:
jan 31 - 19 = 12
dec = 25

total = 365 + 1 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 31 + 30 + 31 + 30 + 12 + 25

with a little expansion, (for example, how do you determine which of the two dates comes first, and how to determine the number of full years between the two dates---the solutions to both questions is similar) I think this technique can be done successfully without problem, too.

8. Originally Posted by mmyers1
I was thinking something along those lines, but am not sure how to go about that. For example if I were to enter 4 4 2004, how many days is it until 12/25/2005? How would I convert 4 4 2004 to an integer? What if the date were 1 1 1980?

This is getting my goat right now.....
4/4/2004 would be day 0.
Count the number of days left in the current month (April) --> total.
Start a loop beginning at the next month and start adding the number of days in each month to total
When you reach Feb, check if leap year and add 1
Stop loop when you finish with Nov 2005
Add the final days for Dec 25