如何检查C ++中的日期格式(dd / mm / yyyy)是否正确?

时间:2018-03-16 01:53:13

标签: c++ sorting date

我制作了一个程序,对日期进行排序并按年份显示。 0 +年,10 +年,20 +年等我也验证了日期。例如,不应超过31天或少于1天。我也有闰年,月等的验证。如何检查日期格式是否有效?我希望在输入日期为6/7/2008或6/07/2008时显示错误消息,指出日期格式不正确。格式应为dd / mm / yyyy。

Person People::getPerson()
    {
        Person person;
        Validation validate;
        string input;
        bool ch;
        int memNo = 0;

        cout << " \nEnter another Person\n" << "Membership Number: ";
        cin >> memNo;
        person.SetMemNo(memNo);
        //While the input entered is not an integer, prompt the user to enter an integer.
        while(cin.fail())

        {
            cin.clear();

            cin.ignore(numeric_limits<int>::max(), '\n');


            cout << "Invalid data. Please type an integer! " << endl;
            cout << "Membership Number: ";
                cin >> memNo;

                cout << endl;




        }


        cout << "Enter first name of person: ";
        cin >> input;
        bool onlystring = validate.IsDigitsOnly(input);

                  //validate string for only alphabets,no                                                                           numbers

            if (onlystring == true) {
                cout << "Enter only alphabets for first name" << endl;
                cout << "Enter first name of person: ";
                cin.clear();
                cin.ignore();
                cin >> input;
            }

        person.SetFirstName(input);

        cout << "Enter your Surname ";
        cin >> input;
        person.SetLastName(input);
        bool onlystring1 = validate.IsDigitsOnly(input);          //validate string for only alphabets,no                                                                       numbers

            if (onlystring1 == true) {
                cout << "Enter only alphabets for last name" << endl;
                cout << "Enter your Surname ";
                cin >> input;
            }
            bool valid_date = false;
            do {
                cout << "Enter your date of joining(DD/MM/YYYY): ";
                cin >> input;


                string new_date= validate.checkFormat(input);
                valid_date = validate.valDate(new_date);
                if (valid_date == true)
                    person.SetDateJoined(new_date);

                else 
                    cout << "Invalid Date!Please  re-enter date of joining!" << endl;


            } while(valid_date == false);

日期验证

bool Validation::valDate(string input)
{
    int Y = stoi(input.substr(6));
    int M = stoi(input.substr(3, 2));
    int D = stoi(input.substr(0, 2));
    //check year
    if (Y >= 1900 && Y <= 9999)
    {
        //check month
        if (M >= 1 && M <= 12)
        {
            //check days
            if ((D >= 1 && D <= 31) && (M == 1 || M == 3 || M == 5 || M == 7 || M == 8 || M == 10 || M == 12)) {

                return true;
            }
            else if ((D >= 1 && D <= 30) && (M == 4 || M == 6 || M == 9 || M == 11)) {

                return true;
            }
            else if ((D >= 1 && D <= 28) && (M == 2)) {

                return true;
            }
            else if (D == 29 && M == 2 && (Y % 400 == 0 || (Y % 4 == 0 && Y % 100 != 0))) {

                return true;
            }
            else {

                return false;
            }
        }
        else
        {

            return false;
        }
    }
    else
    {
        printf("Year is not valid.\n");
        return false;
    }

    return 0;
}


int Validation::Prepend_function ( int n) {
    if (n < 10 && n < 0) {
        string num = to_string(n);
        string new_num = "0" + num;
        int number = stoi(new_num);
        return number;

    }
    else {
        return n;
    }
}



string Validation::checkFormat(string input)

{

    //check the length of the string

    int len = input.size();

    if (len != 10)

    {
        int year = stoi(input.substr(4));
        int month = stoi(input.substr(2, 1));
        int day = stoi(input.substr(0, 1));

        int prepend_day = Prepend_function(day);
        int prepend_month = Prepend_function(month);
        string day1 = to_string(prepend_day);

        string month1 = to_string(prepend_month);
        string year1 = to_string(year);
        string date = day1+"/"+month1+"/"+year1;
        return date;

    }
    }

1 个答案:

答案 0 :(得分:0)

我想您正在询问如何实施Validation::checkFormat,因为Validation::valDate似乎不正确。

std::string Validation::checkFormat(std::string input)
{
    std::stringstream in(input);
    int day, month, year;
    char ignore;
  // Match num/num/num, otherwise invalid
    if (in >> day >> ignore >> month >> ignore >> year)
    {
        std::stringstream out;
      // Force leading 0s
        out << std::setfill('0') << std::setw(2) << day << '/' << month << '/' <<  std::setw(4) << year;
        return out.str();
    }

  // have to return something if it isn't valid
    return std::string();
}

请注意,checkFormat听起来是错误的名称,用于返回以特定方式格式化的字符串。我会将其称为fixFormat,并将inputfixFormat的结果进行比较。如果它们相同,则input采用指定的格式