Note that there are some explanatory texts on larger screens.

plurals
  1. PONeed to change the c++ code
    primarykey
    data
    text
    <p>I wrote this code for date validation.. It works fine.. but i need to send a string and should get in this function and validate the string and return it back. how i can change the code...</p> <p>if i remove static and use prototype variables i am not getting desired output.. for ex..</p> <pre><code>main() { dobvalidation(b); } void dobvalidation(string b) { //validates } </code></pre> <p>i need in above format.. this is my code</p> <pre><code>#include &lt;iostream&gt; #include &lt;string&gt; #include &lt;cstring&gt; #include &lt;stdlib.h&gt; #include &lt;ctime&gt; using namespace std; void checkFormat(); void dobValidation(); static string input; int main() { cout&lt;&lt;"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&lt;&lt;"\nPlease enter a valid Date of Birth\n"; cin&gt;&gt;input; checkFormat(); return; } char * val; val = const_cast&lt;char*&gt;((input.substr(2,1)).c_str()); //check for the dashes in dob if(strcmp(val,"-")!=0) { cout&lt;&lt;"\nPlease enter a valid Date of Birth\n"; cin&gt;&gt;input; checkFormat(); return; } val = const_cast&lt;char*&gt;((input.substr(5,1)).c_str()); if(strcmp(val,"-")!=0) { cout&lt;&lt;"\nPlease enter a valid Date of Birth\n"; cin&gt;&gt;input; checkFormat(); return; } //check for digits //extract date from string char * date; date = const_cast&lt;char*&gt;((input.substr(0,2)).c_str()); //check char by char for numeric char c; for(int i=0;i&lt;2;i++) { c = date[i]; if(!isdigit(c)) { cout&lt;&lt;"\nPlease enter a valid Date of Birth\n"; cin&gt;&gt;input; checkFormat(); return; } } //extract month from string char * month; month = const_cast&lt;char*&gt;((input.substr(3,2)).c_str()); //check char by char for numeric for(int i=0;i&lt;2;i++) { c = month[i]; if(!isdigit(c)) { cout&lt;&lt;c; cout&lt;&lt;"\nPlease enter a valid Date of Birth\n"; cin&gt;&gt;input; checkFormat(); return; } } //extract year from string char * year; year = const_cast&lt;char*&gt;((input.substr(6,4)).c_str()); //check char by char for numeric for(int i=0;i&lt;4;i++) { c = year[i]; if(!isdigit(c)) { cout&lt;&lt;"\nPlease enter a valid Date of Birth\n"; cin&gt;&gt;input; checkFormat(); return; } } return; } void dobValidation() { // cout&lt;&lt;dob; //date char * date1; date1 = const_cast&lt;char*&gt;((input.substr(0,2)).c_str()); int dd=atoi(date1); //month char * month1; month1 = const_cast&lt;char*&gt;((input.substr(3,2)).c_str()); int mm=atoi(month1); //year char * year1; year1 = const_cast&lt;char*&gt;((input.substr(6,4)).c_str()); int yyyy=atoi(year1); //cout&lt;&lt;dd&lt;&lt;mm&lt;&lt;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 &amp;&amp; yyyy%4 == 0) ) &amp;&amp; mm==1) { max_no_of_day=29; } // check date doesnt cross the max limit if(dd &gt; max_no_of_day || dd&lt;1) { // cout&lt;&lt;"max"&lt;&lt;max_no_of_day&lt;&lt;endl; // cout&lt;&lt;dd&lt;&lt;mm&lt;&lt;yyyy; cout&lt;&lt;"\nPlease enter a valid Date of Birth\n"; cin&gt;&gt;input; dobValidation(); return; } // month validation if(mm &gt;12 || mm&lt;1) { cout&lt;&lt;"\nPlease enter a valid Date of Birth\n"; cin&gt;&gt;input; dobValidation(); return; } //year verification time_t t = time(0); // get time now struct tm * now = localtime( &amp; t ); //convert to local time int current_year = (now-&gt;tm_year + 1900); int current_month = (now-&gt;tm_mon + 1); int current_date = (now-&gt;tm_mday); // date should not exceed current date if(yyyy==current_year &amp;&amp; mm&gt;current_month) { cout&lt;&lt;"\nPlease enter a valid Date of Birth\n"; cin&gt;&gt;input; dobValidation(); return; } if(yyyy==current_year &amp;&amp; mm==current_month &amp;&amp; dd&gt;current_date) { cout&lt;&lt;"\nPlease enter a valid Date of Birth\n"; cin&gt;&gt;input; dobValidation(); return; } //check whether year crossed current year if(yyyy&gt;current_year || yyyy&lt;1900) { cout&lt;&lt;"\nPlease enter a valid Date of Birth\n"; cin&gt;&gt;input; dobValidation(); return; } return; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload