简体   繁体   中英

C++ Runtime Function Hang

After much error shooting, I've got a compiling program that runs, but encounters an issue.

It will hang at a certain point, when it seems to call a function.

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 */

Under the main.ccp, the program seems to hang at this point:

} else {
        while (trial.precedes(date)) {
            trial.advance();
            weekday = (weekday + 1) % 7;
        }

If I enter values before 'trial', (Trial is 1 1 1). If I enter 0 0 0, the correct output is displayed of 'Mystery day'.

However any values after the trial value, the program will hang.

It seems to me there is an error somewhere in the Date.ccp on one of the functions such as isLeapYear, advance, or daysInMonth.

Any ideas?

Seems like the advance function

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

   // lots of checks

}

doesn't advance anything.

That will cause the while-condition to never become true.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM