HOW TO MAKE DATE VALIDATOR PROGRAM IN C++

can you give me a sample program that will make a date validator please? dont have any idea?..im really new here...tnx hope someone response..
Here's a small portion of a datetime class I made once:

Note that this assumes all dates use the gregorian calendar, so dates prior to ~1582 might give incorrect results. I'm not completely sure how that whole calendar conversion stuff altered results, but anyway. It should work for recent dates, which is probably what you need it for.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
bool isleapyear(unsigned short year){
	return (!(year%4) && (year%100) || !(year%400));
}

//1 valid, 0 invalid
bool valid_date(unsigned short year,unsigned short month,unsigned short day){
	unsigned short monthlen[]={31,28,31,30,31,30,31,31,30,31,30,31};
	if (!year || !month || !day || month>12)
		return 0;
	if (isleapyear(year) && month==2)
		monthlen[1]++;
	if (day>monthlen[month-1])
		return 0;
	return 1;
}
Boost Date Time. Will be more than sufficient.


http://www.boost.org/doc/libs/1_35_0/doc/html/date_time.html

tnx for the response it helps me
hi thanks for your help... now i got it right... my program runs very ok...
but the last part is not yet ok... about the "do you want to try again?" i want to return it to the start so that can able to try the program again... my code is not working can you help me to fix it? tnx...





#include <iostream.h>
#include <conio.h>

//Main Function

int main()
{

int days,month,dd,mm,yy,num;
char ans,again;

do{
cout<<"Im Data Validator!Wanna Try me?! (Y/N) \n";
cin>>ans;
}while(ans=='N'||ans=='n');

do{
cout<<"Enter Date: \n";
cin>> dd;
}while (dd==1&&dd==31);

do{
cout<<"Enter Month: \n";
cin>> mm;
}while (mm==1&&mm==12);

do{
cout<<"Enter Year: \n";
cin>> yy;
}while (yy==0&&yy==num);


// Function

switch (mm){
case 1: case 3: case 5: case 7:case 8: case 10: case 12 : days=31;
break;
case 4: case 6: case9: case 11 : days=30;
break;
case 2 : if (yy%4==0)days=29;
else
days=28;
}

// Function

if (dd>=1&&dd<=days&&mm>=1&&mm<=month)
cout<<"Your Date is Valid!! \n";
else
cout<< "Sorry Your Date is Invalid \n";

do{
cout<<"Do you want to try it again? (Y/N)";
cin>>again;

}while (again=='N'||again=='n');


getch();

return 0;

}
Hello !

1. The idea to ask questions in loops is not a good one. For example in your sample the program will never come out of this code if the unswer is 'n' or 'N':

1
2
3
4
do{
cout<<"Im Data Validator!Wanna Try me?! (Y/N) \n";
cin>>ans;
}while(ans=='N'||ans=='n');


The execution will be like this:

Im Data Validator!Wanna Try me?! (Y/N)
n
Im Data Validator!Wanna Try me?! (Y/N)
n
Im Data Validator!Wanna Try me?! (Y/N)
n
....................... and so on

so you'd better do like this

1
2
3
4
cout<<"Im Data Validator!Wanna Try me?! (Y/N) \n";
cin>>ans;
if (ans == 'n' || ans == 'N')
    return;


So the program will continue its execution only if the answer is not 'n' (or 'N').

2. The program itself checks the validity of date, so no need to check it at the input, just write:

1
2
3
4
5
6
7
8
cout<<"Enter Date: \n";
cin>> dd;

cout<<"Enter Month: \n";
cin>> mm;

cout<<"Enter Year: \n";
cin>> yy;


Even if you want to check them, your condition expressions are incorrect, so for example for month it can be:

1
2
3
4
do{
cout<<"Enter Month: \n";
cin>> mm;
}while (mm<1 || mm>12);


i.e. the program prompts for month input until it is in the range [1;12].

3. If you want to run the program again, after its first execution, move the whole code to a separate function, and execute it in the loop:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
bool isDateValid(int day, int month, int year)
{
    // returns true if the date is valid and false in the opposite case.
    ..................
}

int main()
{
    char ans;
    int dd, mm, yy;

    do {
        cout<<"Enter Date: \n";
        cin>> dd;

        cout<<"Enter Month: \n";
        cin>> mm;

        cout<<"Enter Year: \n";
        cin>> yy;

        if (isDateValid(dd, mm, yy))
            cout<<"The date "<<dd<<'/'<<mm<<'/'<<yy<<" is valid"<<endl;
        else
            cout<<"The date "<<dd<<'/'<<mm<<'/'<<yy<<" is invalid"<<endl;

        cout<<"Do you want to try again ??? (Y/N)"<<endl;
        cin>>ans;
    } while (ans != 'n' && ans != 'N');

    return 0;
}
i tried to do it like this...its working ....again there's something wrong with the validation (switch part of it...)
like this;
enter month:4
enter day:31
enter year:2008

the result is Valid but that should be invalid because its april..
likewise other month... i think my switch case is not workin...can you help me figure it out?..

same as "do you want to try again.."
if i put bool its error...my compiler is turbo c only...




#include <iostream.h>
#include <conio.h>


int main()
{

int day,dd,mm,yy,num;
char ans;
clrscr();

do{
cout<<"Enter Month: \n";
cin>> mm;

}while (mm<1||mm>12);

do{
cout<<"Enter Day: \n";
cin>> dd;

}while (dd<1||dd>31);

do{
cout<<"Enter Year: \n";
cin>> yy;
}while (yy<1||yy==num);



// validation

switch (mm){
case 1: if (1,3,5,7,8,10,12);
day=31;
break;
case 2: if (4,6,9,11);
day=30;
break;
case 3 :
if(((yy%4==0)&&(yy%100!=0))||(yy%400==0))
day=29;
else
day=28;

}

if (dd, mm, yy)
cout<<"The Date "<<mm<<'/'<<dd<<'/'<<yy<<" is VALID!"<<endl;
else
cout<< "The Date "<<mm<<'/'<<dd<<'/'<<yy<<" is INVALID!"<<endl;

do{
cout<<"do you want to try again?? (Y/N) "<<endl;
cin>>ans;
} while (ans!='n'&&ans!='N');



return 0;

}
Last edited on
Topic archived. No new replies allowed.