It would have been much better if you had posted a part of you code where you are facing problem.
Ok,
Whenever you add days, you do it like this
First make an array say montd which stores the number of days in each month.
Now here the function for adding:
void addDays(int number)
{
days = days + number;
if(days>montd[month])
{
days = days - montd[month];
++month;
if(month>12)
{
month = 1;
++year;
}
}
}
vishesh
Nearly a Posting Virtuoso
1,381 posts since Oct 2006
Reputation Points: 85
Solved Threads: 42
check_month() is incorrect. November has only 30 days, not 31.
class data objects: I think all you need are current_day, current_month, and current_year. You don't really need any of the others. add_years() for example should be coded like this:
//add years to default date
void add_years (int num_years) {
curr_year = curr_year + num_years;
//print
}//end
You need a loop to add months,
while new months > 12
increment year
subtract 12 from months
end of while loop
//Now check the value of day to make sure it does not exceed
//number of days in the new month so that you don't wind up
// with something like 31 February.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
Oh yes picked out mistake in my code. You will also need loop or calculate remainder to increment months. It would work if days are less than the no of days in month but not if larger, eg. 65 days.
vishesh
Nearly a Posting Virtuoso
1,381 posts since Oct 2006
Reputation Points: 85
Solved Threads: 42
it might work but it could be a little more efficient. Don't need variable months and you don't need that if statement, just the while loop.
Month = Month + nun_months;
while(Month > 12)
{
Year++;
Month = Month - 12;
}
Now after that code you need to verify that Day is in range for the given Month.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
just the opposite of adding them
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
Depends on how the dates are formatted. If they are in YYYYMMDD format then you can simply use strcmp()
char *date1 = "2010/01/01";
char *date2 = "2010/06/05";
int n = stramp(date1,date2);
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343