So, I've been trying to validate CNIC because Pakistan requires male CNIC's to end in odd number and female CNIC's to end in even number. CNIC is stored as a string in a structure containing information about a bank user. The problem is when i apply the logic, it doesn't seem to work as intended and always prints the invalid CNIC prompt even when it is correctly input. Here is the relevant code:
struct account//The structure used to store the records
{
int mm,dd,yyyy,Deposit;
long long int accountnum;
char name[50];
char address[50];
char Account[50];
char CNIC[50];
char Num[50];
char gender;
} s[100];
void ValidCNIC(int j) {
int i = 0, check = 0;
char ch;
printf("\n \tCNIC: ");
fgets(s[j].CNIC,sizeof(s[j].CNIC),stdin);
fflush(stdin);
while(i < strlen(s[j].CNIC)-1) {
ch = s[j].CNIC[i];
if(ch >= '0' && ch <= '9') {
i++;
}
else{
printf(" \tThe CNIC can contain numbers only!\n");
fordelay(1000000000);
ValidCNIC(j);
}
}
if (strnlen(s[j].CNIC,sizeof(s[j].CNIC)) < 14)
{
printf(" \tThe CNIC is too short\n \tPlease reenter\n");
fordelay(1000000000);
ValidCNIC(j);
}
else if (strnlen(s[j].CNIC,sizeof(s[j].CNIC)) > 14) {
printf(" \tThe CNIC is too long\n \tPlease reenter\n");
fordelay(1000000000);
ValidCNIC(j);
}
int len = strnlen(s[j].CNIC,sizeof(s[j].CNIC));
if((s[j].gender == 'm') && ((s[j].CNIC[len] !='1')||(s[j].CNIC[len] !='3')||(s[j].CNIC[len] !='5')||(s[j].CNIC[len] !='7')||(s[j].CNIC[len] !='9')))
{
printf("invalid CNIC, male CNIC must always end in an odd number");
ValidCNIC(j);
}
else if((s[j].gender == 'w') && ((s[j].CNIC[len] !='0')||(s[j].CNIC[len] !='2')||(s[j].CNIC[len] !='4')||(s[j].CNIC[len] !='6')||(s[j].CNIC[len] !='8')))
{
printf("Invalid CNIC, female CNIC must end in an even number");
ValidCNIC(j);
}