C++ program for date validation using switch case.

#include<iostream.h>
#include<conio.h>
void main(){
int dd,mm,yyyy,md,flag=1;
clrscr();
cout<<"Enter date in this format"<<endl<<"dd/mm/yyyy"<<endl;
cin>>dd>>mm>>yyyy;
switch (mm) {
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
md=31; break;
case 4: case 6: case 9: case 11:
md=30; break;
case 2:
if (yyyy%4==0)
md=29;
else md=28;
break;
default: flag=0;
cout<<endl<<"Invalid month "<<mm;
}
if (flag==1) {
if (yyyy>=1900 && yyyy<=2100) {
if (dd>=1 && dd<=md)
cout<<"Given date "<<dd<<"/"<<mm<<"/"<<yyyy <<" is vaid date"<<endl;
else cout<<"Invalid day "<<dd;
}
else cout<<yyyy<<" is not in given year range 1900 to 2100 "<<endl;
}
getch();
}

No comments:

Post a Comment