do while validation problem
Hey guys having problems with my do while loop. I am trying to do some validation which works but however i am trying to ask the input to repeat itself if the input is invalid. But if it is valid break out of the do while and proceed with the rest of the code. I have no idea what i can put in the while(); part to do this.
Code:
printf("\nPlease Enter your age in dd/mm/yyyy format:");
fgets(input, MAXDATE, stdin);
do
{
if ( fgets ( input, sizeof input, stdin ) != NULL )
{
printf("is an invalid date!");
}
if ( sscanf ( input, "%d/%d/%d", &m, &d, &y ) == 3 )
{
if ( is_valid ( m, d, y ) )
{
printf ( "%d/%d/%d is a valid date\n", m, d, y );
}
}
else
{
printf ( "Invalid date\n" );
}
}
while(valid ==1 || input !=1);
}
int is_valid ( int m, int d, int y )
{
static const int mday[] = {
0,31,28,31,30,31,30,31,31,30,31,30,31
};
if ( m <= 0 || m > 12 )
return 0;
else if ( d <= 0 || d > mday[m] )
return 0;
else if ( y < 1900 || y > 3000 ) /* Arbitrary choices */
return 0;
return 1;
}