需要更改c++代码
Need to change the c++ code
我写这段代码是为了进行日期验证。。它很好用。。但是我需要发送一个字符串,并且应该进入这个函数,验证字符串并将其返回。如何更改代码。。。
如果我删除静态并使用原型变量,我就不会得到所需的输出。。例如
main()
{
dobvalidation(b);
}
void dobvalidation(string b)
{
//validates
}
我需要上面的格式。。这是我的代码
#include <iostream>
#include <string>
#include <cstring>
#include <stdlib.h>
#include <ctime>
using namespace std;
void checkFormat();
void dobValidation();
static string input;
int main()
{
cout<<"Enter date of birth (dd-mm-yyyy)n";
getline(cin,input,'n');
checkFormat();
dobValidation();
return 0;
}
void checkFormat()
{
//check the length of the string
int len=input.size();
if(len!=10)
{
cout<<"nPlease enter a valid Date of Birthn";
cin>>input;
checkFormat();
return;
}
char * val;
val = const_cast<char*>((input.substr(2,1)).c_str());
//check for the dashes in dob
if(strcmp(val,"-")!=0)
{
cout<<"nPlease enter a valid Date of Birthn";
cin>>input;
checkFormat();
return;
}
val = const_cast<char*>((input.substr(5,1)).c_str());
if(strcmp(val,"-")!=0)
{
cout<<"nPlease enter a valid Date of Birthn";
cin>>input;
checkFormat();
return;
}
//check for digits
//extract date from string
char * date;
date = const_cast<char*>((input.substr(0,2)).c_str());
//check char by char for numeric
char c;
for(int i=0;i<2;i++)
{
c = date[i];
if(!isdigit(c))
{
cout<<"nPlease enter a valid Date of Birthn";
cin>>input;
checkFormat();
return;
}
}
//extract month from string
char * month;
month = const_cast<char*>((input.substr(3,2)).c_str());
//check char by char for numeric
for(int i=0;i<2;i++)
{
c = month[i];
if(!isdigit(c))
{
cout<<c;
cout<<"nPlease enter a valid Date of Birthn";
cin>>input;
checkFormat();
return;
}
}
//extract year from string
char * year;
year = const_cast<char*>((input.substr(6,4)).c_str());
//check char by char for numeric
for(int i=0;i<4;i++)
{
c = year[i];
if(!isdigit(c))
{
cout<<"nPlease enter a valid Date of Birthn";
cin>>input;
checkFormat();
return;
}
}
return;
}
void dobValidation()
{
// cout<<dob;
//date
char * date1;
date1 = const_cast<char*>((input.substr(0,2)).c_str());
int dd=atoi(date1);
//month
char * month1;
month1 = const_cast<char*>((input.substr(3,2)).c_str());
int mm=atoi(month1);
//year
char * year1;
year1 = const_cast<char*>((input.substr(6,4)).c_str());
int yyyy=atoi(year1);
//cout<<dd<<mm<<yyyy;
int days[12]={31,28,31,30,31,30,31,31,30,31,30,31};
int max_no_of_day = days[mm-1];
//check for leap year
if((yyyy%400 ==0 || (yyyy%100 != 0 && yyyy%4 == 0) ) && mm==1)
{
max_no_of_day=29;
}
// check date doesnt cross the max limit
if(dd > max_no_of_day || dd<1)
{
// cout<<"max"<<max_no_of_day<<endl;
// cout<<dd<<mm<<yyyy;
cout<<"nPlease enter a valid Date of Birthn";
cin>>input;
dobValidation();
return;
}
// month validation
if(mm >12 || mm<1)
{
cout<<"nPlease enter a valid Date of Birthn";
cin>>input;
dobValidation();
return;
}
//year verification
time_t t = time(0); // get time now
struct tm * now = localtime( & t ); //convert to local time
int current_year = (now->tm_year + 1900);
int current_month = (now->tm_mon + 1);
int current_date = (now->tm_mday);
// date should not exceed current date
if(yyyy==current_year && mm>current_month)
{
cout<<"nPlease enter a valid Date of Birthn";
cin>>input;
dobValidation();
return;
}
if(yyyy==current_year && mm==current_month && dd>current_date)
{
cout<<"nPlease enter a valid Date of Birthn";
cin>>input;
dobValidation();
return;
}
//check whether year crossed current year
if(yyyy>current_year || yyyy<1900)
{
cout<<"nPlease enter a valid Date of Birthn";
cin>>input;
dobValidation();
return;
}
return;
}
在非常高的级别上,我建议如下:
-
不要使用全局
string input
。将输入作为参数传递给函数。 -
避免递归调用函数。返回函数的成功/失败结果,并让主循环决定要做什么。
例如,您的主循环可能如下所示:
int main()
{
while (true) {
cout<<"Enter date of birth (dd-mm-yyyy)n";
string input;
getline(cin,input,'n');
if (!checkFormat(input)) {
cout<<"nPlease enter a valid Date of Birthn";
continue;
}
if (!dobValidation(input)) {
cout<<"nPlease enter a valid Date of Birthn";
continue;
}
cout << "thanks.n";
break;
}
}
一般来说,像static string input
这样的全局变量不是一个好的编码实践。我会这样做:
main() {
string input;
bool isValid = false;
cout<<"Enter date of birth (dd-mm-yyyy)n";
getline(cin,input,'n');
while(!checkFormat(input) || !dobValidation(input)) {
cout<<"Please enter a valid date of birth (dd-mm-yyyy)n";
getline(cin,input,'n');
}
return 0;
}
bool checkFormat(string input) {
// return true if format is valid, false otherwise
}
bool dobValidation(string input) {
// return true if dob is valid, false otherwise
}
在较低的级别上,您可以通过使用c++来简化很多。例如
//extract year from string
char * year;
year = const_cast<char*>((input.substr(6, 4)).c_str());
//check char by char for numeric
for (int i = 0;i < 4;i++)
{
c = year[i];
if (!isdigit(c))
{
cout << "nPlease enter a valid Date of Birthn";
cin >> input;
checkFormat();
return ;
}
}
可以成为
//check year for numerics
for (int i = 6; i < 10; i++)
{
if (! isdigit(input[i]))
{
return false;
}
}
Greg为您提供了最高级别,所以我将为您指出正确的方向:strptime
。
这是一个Unix实用程序,不过我想如果你对Windows支持感兴趣的话,它可能也有类似的功能。
所使用的格式字符串与它的姊妹strftime
的格式字符串相似,可以在这里查阅。
在您的情况下:
bool checkFormat(std::string const& str, tm& date) {
char const* const s = strptime(str.c_str(), "%d-%m-%Y", &date);
return s != NULL; // NULL indicates failure
}
注意有两个(记录在案的)怪癖:
tm.tm_year // number of years since 1900
tm.tm_mon // month in [0..11] (where 0 is January and 11 is December)
阅读struct tm
文档,了解有关精确表示的更多信息。
相关文章:
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 代码在main()中运行,但在函数中出现错误
- 在VS代码中交叉编译Windows与Linux上的MinGW的SDL程序
- 编译包含字符串的代码时遇到问题
- 我在c++代码中生成了一个运行时#3异常
- 如何在linux终端中同时编译和运行c++代码
- 为cl.exe(Visual Studio代码)指定命令行C++版本
- 在Linux for Windows上编译C++代码时出错
- 我的字符计数代码计算错误.为什么
- 孤立代码块在结构中引发异常
- 在编译C++代码(具有dlib和opencv)到WASM时面临问题
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- 此代码是否违反一个定义规则
- 为什么我的代码在输出中增加了93天
- 我的简单if-else语句是如何无法访问的代码
- 使用动态分配的数组会导致代码分析发出虚假的C6386缓冲区溢出警告
- 为什么在这个代码结束循环中没有得到结束
- 在c代码之间共享数据的最佳方式
- 这个指针和内存代码打印是什么?我不知道是打印垃圾还是如何打印我需要的值