2012-02-14 98 views
-1

我爲日期驗證編寫了此代碼..它工作正常..但我需要發送一個字符串,並應獲取此函數並驗證該字符串並將其返回。我怎麼可以更改代碼...需要更改C++代碼

如果我刪除靜態和使用原型變量我沒有得到對前所需的輸出..

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 Birth\n"; 
     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 Birth\n"; 
     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 Birth\n"; 
     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 Birth\n"; 
      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 Birth\n"; 
      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 Birth\n"; 
      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 Birth\n"; 
       cin>>input; 
       dobValidation(); 
     return; 
     } 

     // month validation 

     if(mm >12 || mm<1) 
     { 
       cout<<"\nPlease enter a valid Date of Birth\n"; 
       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 Birth\n"; 
       cin>>input; 
       dobValidation(); 
     return; 
    } 

    if(yyyy==current_year && mm==current_month && dd>current_date) 
    { 
     cout<<"\nPlease enter a valid Date of Birth\n"; 
       cin>>input; 
       dobValidation(); 
     return; 
    } 

    //check whether year crossed current year 
    if(yyyy>current_year || yyyy<1900) 
    { 
     cout<<"\nPlease enter a valid Date of Birth\n"; 
       cin>>input; 
       dobValidation(); 
     return; 
    } 


return; 
     } 
+0

不要const_cast。將你的變量從'char *'改成''const char *' – balki 2012-02-14 06:33:53

+1

或者只是在不需要的時候使用C字符串,並且完全避免使用C字符串。 – Duck 2012-02-14 06:39:20

回答

4

在一個非常高的水平,這是我建議:

  • 不要使用全球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 Birth\n"; 
      continue; 
     } 
     if (!dobValidation(input)) { 
      cout<<"\nPlease enter a valid Date of Birth\n"; 
      continue; 
     } 
     cout << "thanks.\n"; 
     break; 
    } 
} 
+0

是的,我明白..我會嘗試 – srinathmkce 2012-02-15 04:49:56

0

一般來說,像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 
} 
0

在一個較低的水平,你可以通過堅持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 Birth\n"; 
     cin >> input; 
     checkFormat(); 
     return ; 
    } 

} 

可以成爲

//check year for numerics 
for (int i = 6; i < 10; i++) 
{ 
    if (! isdigit(input[i])) 
    { 
     return false; 
    } 
} 
0

格雷格給你的頂層,所以我會在對膽量指向正確的方向你:strptime

這是一個Unix實用程序,但我想如果您對Windows支持感興趣,它可能具有相同的功能。

使用的格式字符串與其姐妹的strftime類似,可以是consulted here

你的情況:

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文檔的確切表示更多的信息。