Im have been trying to figure for the last couple of days a way to add a certain number of days to a set date and make that the new set date. Here is what I a have so far:
#include <iostream>
#include "dateType.h"
usingnamespace std;
void dateType::setDate(int month, int day, int year)
{
if(month < 1 || month > 12)
{
dMonth = 1;
cout << "Invalid month set. Defaulted to 1." << endl;
}
else
dMonth = month;
dYear = year;
switch(dMonth)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
if(day < 1 || day > 31)
{
dDay = 1;
cout << "Invalid day set. Defaulted to 1." << endl;
}
else
dDay = day;
break;
}
switch(dMonth)
{
case 2:
if(dateType::isLeapYear())
{
if(day < 1 || day > 29)
{
dDay = 1;
cout << "Invalid day set. Defaulted to 1." << endl;
}
else
dDay = day;
}
else
{
if(day < 1 || day > 28)
{
dDay = 1;
cout << "Invalid day set. Defaulted to 1." << endl;
}
else
dDay = day;
}
break;
case 4:
case 6:
case 9:
case 11:
if(day < 1 || day > 30)
{
dDay = 1;
cout << "Invalid day set. Defaulted to 1." << endl;
}
else
dDay = day;
break;
}
}
void dateType::setMonth(int month)
{
dMonth = month;
}
void dateType::setDay(int day)
{
dDay = day;
}
void dateType::setYear(int year)
{
dYear = year;
}
int dateType::getDay()
{
return dDay;
}
int dateType::getMonth()
{
return dMonth;
}
int dateType::getYear()
{
return dYear;
}
int dateType::getDaysInMonth()
{
switch(dMonth)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
dDaysInMonth = 31;
}
switch(dMonth)
{
case 2:
if(dateType::isLeapYear())
{
dDaysInMonth = 29;
}
else
{
dDaysInMonth = 28;
}
break;
case 4:
case 6:
case 9:
case 11:
dDaysInMonth = 30;
break;
}
return dDaysInMonth;
}
int dateType::numberOfDaysPassed()
{
int count = dMonth;
int total = 0;
dMonth = 0;
for(int i = 0; i < count; i++)
{
dMonth++;
total += dateType::getDaysInMonth();
}
return total - (dateType::getDaysInMonth() - dDay);
}
int dateType::numberOfDaysLeft()
{
int total = 0;
if(dateType::isLeapYear())
total = 366 - dateType::numberOfDaysPassed();
else
total = 365 - dateType::numberOfDaysPassed();
return total;
}
void dateType::incrementDate(int num)
{
}
void dateType::printDate()
{
cout << dMonth << "-" << dDay << "-" << dYear;
}
bool dateType::isLeapYear()
{
if(dYear % 4 == 0)
returntrue;
elsereturnfalse;
}
dateType::dateType(int month, int day, int year)
{
if(month < 1 || month > 12)
{
dMonth = 1;
cout << "Invalid month set. Defaulted to 1." << endl;
}
else
dMonth = month;
dYear = year;
switch(dMonth)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
if(day < 1 || day > 31)
{
dDay = 1;
cout << "Invalid day set. Defaulted to 1." << endl;
}
else
dDay = day;
break;
}
switch(dMonth)
{
case 2:
if(dateType::isLeapYear())
{
if(day < 1 || day > 29)
{
dDay = 1;
cout << "Invalid day set. Defaulted to 1." << endl;
}
else
dDay = day;
}
else
{
if(day < 1 || day > 28)
{
dDay = 1;
cout << "Invalid day set. Defaulted to 1." << endl;
}
else
dDay = day;
}
break;
case 4:
case 6:
case 9:
case 11:
if(day < 1 || day > 30)
{
dDay = 1;
cout << "Invalid day set. Defaulted to 1." << endl;
}
else
dDay = day;
break;
}
}
The function incrementDate (the empty function) has been killing me. I have tried multiple things including the following:
1 2 3 4 5 6 7 8 9 10
void dateType::incrementDate(int num)
{
num -= (dateType::getDaysInMonth() - dDay);
do
{
dMonth++;
}
while(num > dateType::getDaysInMonth());
dDay = num;
}
It doesn't take a genius to figure out the above will only work for so many days before it starts incrementing the months too and not adjusting the days...does anyone have guidance for me??
1. Check if adding "num" will overflow the current month. If not, you're done; you just have to add "num" to the current day.
2. If yes, figure out how many days until the next month and subtract that from "num". Now, set the day to the 1st and set the month to the next month. Increment the year if it's now January. Go back to 1.
You could also implement your date internally as an unsignedint representing the days since January 1, year 0. That way, you just have to write a two-way conversion between that number and the month, day, and year. With this approach, incrementDate would be just adding num to the internal value.