views:

88

answers:

4

Hi everyone, I have an assignees that I've been working on and I'm stuck on the last function.

use the function void Increment(int numDays = 1)

This function should move the date forward by the number of calendar days given in the argument. Default value on the parameter is 1 day. Examples:

Date d1(10, 31, 1998); // Oct 31, 1998
Date d2(6, 29, 1950); // June 29, 1950

d1.Increment(); // d1 is now Nov 1, 1998
d2.Increment(5); // d2 is now July 4, 1950

I don not understand how to do this.

void Date::Increment(int numDays = 1)

I'm stuck, I know how to tell the function to increment, by the ++ operator but i get confuse when I have to get the function to increment the last day of the month to the the fist, or to end at the last date of that month for example. Oct 31 to Nov 1, or June 29 to July 4. I can do July 5 to July 8 but the changing months confuse me

A: 

30 days has September, April, June, and November. The rest have 31 days, except for February, which has 28 days except on a leap year (every 4 years, and 2008 was the last one) when it has 29 days.

This should be plenty to get you going.

San Jacinto
A: 

You will need to store a list (or array) of how many days are in each month. If you add numDays to the current date and it becomes bigger than this, you need to increment the month as well.

For example, we have a date object representing 29 March 2010. We call Increment(4) and add 4 to the day variable, ending up with 33 March 2010. We now check how many days March has and find out it's 31 (eg. daysInMonth[3] == 31). Since 33 is greater than 31, we need subtract 31 from 33 and increase the month, ending up with 2 April 2010.

You will need special handling for February in leap years (any year divisible by 4 and not divisible by 100 unless it's also divisible by 400) and for incrementing past the end of December.

Zooba
A: 

First, construct a function like

 int numDaysSinceBeginning( Date );

which counts number of days elapsed from a well known date (e.g. Jan 1 1900) to the specific Date.

Next, construct another function which converts that day-delta to Date

Date createDateWithDelta( int );

From your example,

Date d2(6, 29, 1950); // June 29, 1950

int d2Delta = numDaysSinceBeginnning( d2 );

Date d2Incremented = createDateWithDelta( d2Delta + 5 ); // d2Incremented is July 4, 1950
ArunSaha
A: 

You will find other answers on this site, such as:

http://stackoverflow.com/questions/2344330/algorithm-to-add-or-subtract-days-from-a-date

Tom Slick