2012-03-11 10 views
0

多くのエラーが発生した後、コンパイルするプログラムが実行されていますが、問題が発生しました。C++ランタイム関数ハング

機能を呼び出すようなときは、ある時点でハングします。 main.ccpの下

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]; 
    } 
} 

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」の前に値を入力すると(試しは1 1 1)。 0 0 0を入力すると、「Mystery day」の正しい出力が表示されます。

ただし、試用値の後の値は、プログラムがハングします。

私には、isLeapYear、advance、またはdaysInMonthなどの関数のいずれかでDate.ccpのどこかにエラーがあるようです。

アイデア?

+0

デバッガはあなたに何を伝えましたか? –

答えて

2

は何も進まない、事前の機能

void Date::advance() { 
    this->day_; 

    // lots of checks 

} 

のように思えます。

これは、while条件が決して真にならないようにします。

関連する問題