Date Validation in C++

Posted: February 11, 2012 in Programming
Tags: , , , , ,

The code for validating date is given below

// header section
#include <iostream.h>
#include <string>
#include <cstring>
#include <stdlib.h>
#include <ctime>

//adding namespace for using standard input and output
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<<input;

checkFormat();

return;

}

char * val;

val = const_cast((input.substr(2,1)).c_str());

//check for the dashes in dob

if(strcmp(val,"-")!=0)

{

cout<<input;

checkFormat();

return;

}

val = const_cast((input.substr(5,1)).c_str());

if(strcmp(val,"-")!=0)

{

cout<<input;

checkFormat();

return;

}

//check for digits

//extract date from string

char * date;

date = const_cast((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<<input;

checkFormat();

return;

}

}

//extract month from string

char * month;

month = const_cast((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<<c;

cout<<input;

checkFormat();

return;

}

}

//extract year from string

char * year;

year = const_cast((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<<input;

checkFormat();

return;

}

}

return;

}

void dobValidation()

{

//date

char * date1;

date1 = const_cast((input.substr(0,2)).c_str());

int dd=atoi(date1);

//month

char * month1;

month1 = const_cast((input.substr(3,2)).c_str());

int mm=atoi(month1);

//year

char * year1;

year1 = const_cast((input.substr(6,4)).c_str());

int yyyy=atoi(year1);

//cout&lt;&lt;dd&lt;&lt;mm&lt; max_no_of_day || dd&lt;1)

{

// cout&lt;&lt;"max"&lt;&lt;max_no_of_day&lt;&lt;endl;


cout<<input;

dobValidation();

return;

}

// month validation

if(mm > 12 || mm<1)

{

cout<<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->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<<input;

dobValidation();

return;

}

if(yyyy==current_year && mm==current_month && dd>current_date)

{

cout<<input;

dobValidation();

return;

}

//check whether year crossed current year

if(yyyy>current_year || yyyy<1900)

{

cout<<input;

dobValidation();

return;

}

return;

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s