创建日期函数C ++

时间:2017-03-09 10:49:21

标签: c++ arrays date time

我正在这个时间工作,他们的功能。 我正在尝试使用checkDay函数在特定月份使用正确的数字天数。 但是,这会导致运行时错误,说“已调用abort()”。 我相信这行“day = checkDay(day);”一定有问题。 ,但不知道如何解决它。谢谢!!

void Date::increaseADay()
{
int temp;

second++;
if (second == 60)
{
    second = 0;
    ++minute;
}
if (minute == 60)
{
    minute = 0;
    ++hour;
}
if (hour == 24)
{
    hour = 0;
    ++day;
}

day = checkDay(day);

}





unsigned int Date::checkDay(int testDay) const
{
static const array< int, monthsPerYear + 1 > daysPerMonth =
{ 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

// determine whether testDay is valid for specified month
if (testDay > 0 && testDay <= daysPerMonth[month])
    return testDay;

// February 29 check for leap year 
if (month == 2 && testDay == 29 && (year % 400 == 0 ||
    (year % 4 == 0 && year % 100 != 0)))
    return testDay;

throw invalid_argument("Invalid day for current month and year");
 } // end function checkDay

0 个答案:

没有答案