输出不显示日期是否有效

Output does not show if date is valid or not

本文关键字:是否 有效 日期 显示 输出      更新时间:2023-10-16

我试图输出日期是否有效。Ie 5-6-2013有效,但62-34-2013无效。当我在main中输入一个合法的日期(5,5,2013)到我的对象时,它会通过开关运行并正常工作,但是当我输入一个奇怪的日期(例如,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;

}

试试下面的代码:

#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;

}

你在switch(month)中漏掉了default病例。你还需要移动这个:

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

。如果月份是有效的,但日期不是,这将处理这种情况。您还错过了if块中的{}

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