0
많은 오류가 발생하면 컴파일 프로그램이 실행되지만 문제가 발생합니다.C++ 런타임 기능 정지
기능을 호출 할 때 특정 시점에 중단됩니다.
Main.ccp
#include <cstdlib>
#include <iostream>
#include "Date.h"
using namespace ::std;
string weekday(Date date);
int main() {
int day, month, year;
cout << "What date (d m y)? ";
cin >> day >> month >> year;
Date event (day, month, year);
cout << ("That was a " + weekday(event));
return 0;
}
string weekday(Date date) {
const string days[] = {"Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"};
Date trial (1, 1, 1);
int weekday = 6;
if (date.precedes(trial)) {
return "Mysteryday";
} else {
while (trial.precedes(date)) {
trial.advance();
weekday = (weekday + 1) % 7;
}
return days[weekday];
}
}
main.ccp에서 Date.ccp
#include "Date.h"
Date::Date(int day, int month, int year) {
day_ = day;
month_ = month;
year_ = year;
}
int Date::getDay() {
return day_;
}
void Date::setDay (int day) {
day_ = day;
}
int Date::getMonth() {
return month_;
}
void Date::setMonth (int month) {
month_ = month;
}
int Date::getYear() {
return year_;
}
void Date::setYear (int year) {
year_ = year;
}
bool Date::isLeapYear() {
bool lear = false;
if (this->year_ <= 1752) {
if (this->year_ % 4 == 0) {
lear = true;
}
}
else {
lear = false;
}
if (this->year_ > 1752) {
if (this->year_ % 4 == 0 && this->year_ % 100 != 0) {
lear = true;
}
else if (this->year_ % 4 == 0 && this->year_ % 100 == 0 && this->year_ % 400 == 0) {
lear = true;
}
else {
lear = false;
}
}
return lear;
}
int Date::daysInMonth() {
switch (this->month_) {
case 9 :
case 4 :
case 6 :
case 11 :
return 30;
default :
return 31;
case 2 :
return this->isLeapYear() ? 29 : 28;
}
}
void Date::advance() {
this->day_;
if (this->day_ == 3 && this->month_ == 9 && this->year_ == 1752) {
day_ = 14;
month_ = 9;
year_ = 1752;
}
if (this->day_ > this->daysInMonth()) {
this->day_ = 1;
this->month_++;
}
if (this->month_ > 12) {
this->month_ = 1;
this->year_++;
}
}
bool Date::precedes (Date date) {
return this->year_ < date.year_
|| this->year_ == date.year_ && this->month_ < date.month_
|| this->year_ == date.year_ && this->month_ == date.month_ && this->day_ < date.day_;
}
Date.h
#ifndef DATE_H
#define DATE_H
class Date {
public:
Date (int day, int month, int year);
int getDay();
void setDay(int day);
int getMonth();
void setMonth(int month);
int getYear();
void setYear(int year);
bool isLeapYear();
int daysInMonth();
void advance();
bool precedes(Date date);
private:
int day_;
int month_;
int year_;
};
#endif /* DATE_H */
, 프로그램은이 시점에서 중지 할 것 같다 :
} else {
while (trial.precedes(date)) {
trial.advance();
weekday = (weekday + 1) % 7;
}
'trial'전에 값을 입력하면 (trial은 1 1 1). 0 0 0을 입력하면 올바른 출력이 'Mystery day'로 표시됩니다.
그러나 평가판 값 이후의 값은 프로그램이 중단됩니다.
isLeapYear, advance 또는 daysInMonth와 같은 함수 중 하나에서 Date.ccp 어딘가에 오류가있는 것 같습니다.
아이디어가 있으십니까?
디버거에서 무엇을 말 했습니까? –