2012-02-14 5 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

+1

Или просто не используйте C-строки, когда нет необходимости, и вообще избегайте приведения. – Duck

ответ

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

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 для получения дополнительной информации о точном представлении.

Смежные вопросы