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;
}
|