Hey everyone I have a class I am building called date and I've built all my functions to run and done all the necessary error checking in each function. However, my last and final function I have to write is where the # of days passed in as a parameter and then I have to increment the days by that many. And if the user does not increment any days at all in the parameter and leaves it blank, then it automatically increments by one day. I am having trouble with this. So for example if the user was to do this:
1 2 3 4 5 6
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
The function starts out looking like this
1 2 3 4
void Date::Increment(int numDays = 1)
{
}
I know I have to use a for loop to accomplish this. I just don't know how to get it to where the days passed in will go to the next month and then days passed in would go to the next year. I am just not sure where to even start. Any help would be greatly appreciated!
Get it to work for any case of incrementing by a single day. You could make a new function like this:
1 2 3 4 5 6 7 8 9 10
void Date::IncrementSingleDay()
{
//Add one to day
//If the day falls outside the valid range for the month,
// reset day to 1
// increment the month
// If the month goes over 12
// reset month to 1
// increment the year
}
If you get that to work and don't want to do anything fancy with your more general 'increment by x days function', you can just increment by one however many times the parameter dictates.
1 2 3 4 5 6 7
void Date::Increment(int numDays)
{
for (int i = 1; i <= numDays; ++i)
{
IncrementSingleDay();
}
}
oh wow Thank you so much! It worked so easily and it was so simple to write. I was definitely over thinking it. At first I tried using modulus to determine how many days would be left over after the days in the month and all this non sense. But that code was very simple. Thank you for making me think about it in A LOT more simplistic terms!