输出不显示日期是否有效

时间:2016-09-22 07:29:55

标签: c++

我试图输出日期是否有效。我认为5-6-2013是有效的,但62-34-2013不是。当我将一个合法的日期(5,5,2013)放入main中的对象时,它会通过交换机运行正常,但是当我放入一个时髦的日期(例如,80,52,2013)时它只输出日期但没有说"无效的日期" 我在这里错过了什么? (请忽略我在整个代码中的所有明显评论 - 它们帮助我记住我的位置并使其更具可读性)谢谢!

#include <iostream>
#include <cmath>
#include <string>
#include <stdio.h>

using namespace std;

class dateType
{
    // defining the class

public:

    void setDate(int month, int day, int year);
    // Function to set the date

    int getDay() const;

    int getMonth() const;

    int getYear() const;

    void printDate() const;

    dateType(int month, int day, int year);
    dateType();

    bool isLeapYear(int yNumber);
    int dMonth;
    int dDay;
    int dYear;

private:
    int yearCalc;
    // integer to store the year, only numbers above 0 will be valid
    int monthCalc;
    // variable to store the month, January through December (1-12)
    int dayCalc;
    // variable to store the day, this varies depending on the month
    // aka 28 days in February unless it is a leap year, 31 days January, etc.
    int outputDate;
    //
};

// function definitions - setDate needs to check for validity
void dateType::setDate(int month, int day, int year)
{
    bool notValid;
    // we need to use a switch in order to run through each month.

    switch (month)
    {
        // months with 28 days - February (2), unless Leap year - test for Leap year
    case 2:
        cout << "February" << endl;
        if (isLeapYear(year))
            cout << "Leap year - 29 days" << endl;
        if (day > 29)
            notValid = true;
        break;


        // months with 31 days - April (4), June (6), September (9) November(11)
    case 4:
        cout << "April - valid, 30 days" << endl;
        break;
    case 6:
        cout << "June - valid, 30 days" << endl;
        break;
    case 9:
        cout << "September - valid, 30 days" << endl;
        break;
    case 11:
        cout << "November - valid, 30 days" << endl;
        if (day > 30)
            notValid = true;
        break;

        // months with 31 days - January (1), March (3), May (5) July(7), August (8), October (10), December (12)
        // will not incude break statement as we do not want to break
        // out of switch until the 31 day checks have been run through
    case 1:
        cout << "January - valid, 31 days" << endl;
        break;
    case 3:
        cout << "March - valid, 31 days" << endl;
        // printing out as a test to validate these months
        break;
    case 5:
        cout << "May - valid, 31 days" << endl;
        break;

    case 7:
        cout << "July - valid, 31 days" << endl;
        break;

    case 8:
        cout << "August - valid, 31 days" << endl;
        break;

    case 10:
        cout << "October - valid, 31 days" << endl;
        break;

    case 12:
        cout << "December - valid, 31 days" << endl;
        if (day >= 32)
            notValid = true;
        cout << "This is not a valid entry" << endl;
        break;
        // now break as we have checked all months ending in 31 days


    }
    dMonth = month;
    dDay = day;
    dYear = year;
}

int dateType::getDay() const
{
    return dDay;
}

int dateType::getMonth() const
{
    return dMonth;
}

int dateType::getYear() const
{
    return dYear;
}

void dateType::printDate() const
{
    cout << dMonth << "-" << dDay << "-" << dYear;

}

dateType::dateType(int month, int day, int year)
{
    setDate(month, day, year);
}

bool dateType::isLeapYear(int yNumber)

{

    if ((yNumber % 4 == 0 && yNumber % 100 != 0) || (yNumber % 400 == 0))
        return true;
    else
        return false;
}

int main()
{
    int day;
    int month;
    int year;

    dateType year1(80, 52, 2013);
    cout << "Date: ";
        year1.printDate();

        system("pause");
        return 0;


}

2 个答案:

答案 0 :(得分:0)

试试这段代码:

#include <iostream>
#include <cmath>
#include <string>
#include <stdio.h>

using namespace std;

class dateType {
  // defining the class

public:

  void setDate(int month, int day, int year);
  // Function to set the date

  int getDay() const;

  int getMonth() const;

  int getYear() const;

  void printDate() const;

  dateType(int month, int day, int year);
  dateType();

  bool isLeapYear(int yNumber);
  int dMonth;
  int dDay;
  int dYear;

private:
  int yearCalc;
  // integer to store the year, only numbers above 0 will be valid
  int monthCalc;
  // variable to store the month, January through December (1-12)
  int dayCalc;
  // variable to store the day, this varies depending on the month
  // aka 28 days in February unless it is a leap year, 31 days January, etc.
  int outputDate;
  //
};

// function definitions - setDate needs to check for validity
void dateType::setDate(int month, int day, int year) {
  bool notValid;
  // we need to use a switch in order to run through each month.

  switch (month) {
    // months with 28 days - February (2), unless Leap year - test for Leap year
  case 2:
    cout << "February" << endl;
    if (isLeapYear(year))
      cout << "Leap year - 29 days" << endl;
    if (day > 29)
      notValid = true;
    break;


    // months with 31 days - April (4), June (6), September (9) November(11)
  case 4:
    cout << "April - valid, 30 days" << endl;
    break;
  case 6:
    cout << "June - valid, 30 days" << endl;
    break;
  case 9:
    cout << "September - valid, 30 days" << endl;
    break;
  case 11:
    cout << "November - valid, 30 days" << endl;
    if (day > 30)
      notValid = true;
    break;

    // months with 31 days - January (1), March (3), May (5) July(7), August (8), October (10), December (12)
    // will not incude break statement as we do not want to break
    // out of switch until the 31 day checks have been run through
  case 1:
    cout << "January - valid, 31 days" << endl;
    break;
  case 3:
    cout << "March - valid, 31 days" << endl;
    // printing out as a test to validate these months
    break;
  case 5:
    cout << "May - valid, 31 days" << endl;
    break;

  case 7:
    cout << "July - valid, 31 days" << endl;
    break;

  case 8:
    cout << "August - valid, 31 days" << endl;
    break;

  case 10:
    cout << "October - valid, 31 days" << endl;
    break;

  case 12:
    cout << "December - valid, 31 days" << endl;
    break;
    // now break as we have checked all months ending in 31 days
  default:
    cout << "This is not a valid entry" << endl;
    break;

  }
  if (day >= 32) {
       notValid = true;
       cout << "This is not a valid entry" << endl;
  }
  dMonth = month;
  dDay = day;
  dYear = year;
}

int dateType::getDay() const {
  return dDay;
}

int dateType::getMonth() const {
  return dMonth;
}

int dateType::getYear() const {
  return dYear;
}

void dateType::printDate() const {
  cout << dMonth << "-" << dDay << "-" << dYear;

}

dateType::dateType(int month, int day, int year) {
  setDate(month, day, year);
}

bool dateType::isLeapYear(int yNumber)

{

  if ((yNumber % 4 == 0 && yNumber % 100 != 0) || (yNumber % 400 == 0))
    return true;
  else
    return false;
}

int main() {
  int day;
  int month;
  int year;

  dateType year1(80, 52, 2013);
  cout << "Date: ";
  year1.printDate();

  system("pause");
  return 0;


}

default您错过了switch(month)个案。你还必须移动它:

if (day >= 32) {
    notValid = true;
    cout << "This is not a valid entry" << endl;
}
切换块后

。如果月份有效,但是这一天不会处理这种情况。您在if块中也错过了{}

答案 1 :(得分:0)

如果月份不在您指定的某个值中,则除了设置月份外,不会执行任何操作。因此,对于52,因为没有case 52default,它只是将月份设置为52,这就是全部。

相关问题