如何检查日期格式(日/月/年)是否正确C++
How to check if date format(dd/mm/yyyy) is correct in C++?
我制作了一个程序,可以对日期进行排序并按年份显示它们。 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 Personn" << "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;
}
}
我想你在问如何实现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
,并将input
与fixFormat
的结果进行比较。如果它们相同,则input
采用指定的格式
相关文章:
- 在提升multi_index容器中,是否定义了"default index"?
- 在C++STL中是否有Polyval(Matlab函数)等价物?
- 检查输入是否不是整数或数字
- 是否可以初始化不可复制类型的成员变量(或基类)
- 在C++中,是否可以基于给定的标识符创建基类的新实例,反之亦然
- 是否可以通过C++扩展强制多个python进程共享同一内存
- 此代码是否违反一个定义规则
- 是否需要删除包含对象的"pair"?
- 是否可以从int转换为enum类类型
- 无论条件是否为true,if总是在c++中执行
- 如何找到大小'x'数组是否完全填充,在C++?
- 检查值是否在集合p1和p2中,但不在p3中
- 是否可以在编译时初始化数组,以便在运行时不会花费时间?
- 检查 std::shared_ptr<> 的当前底层类型是否为 T
- 在c++中检查长方体是否尽可能快地重叠(无迭代)
- GL_SHADERSTORAGE_BUFFER位置是否与其他着色器位置冲突
- 子目录是否继承属性,例如add_definitions,include_directories和父Cmakelist.t
- 标准是否使用多余的大括号(例如 T{{{10}}})定义列表初始化?
- C/C++预处理器是否可以检测一些编译器选项
- 是否可以用"iostream"包装现有的TCP/OOpenSSL会话