How to check if a code contains 3 letters and 4 numbers in this order?

Asked

Viewed 51 times

0

I am assigning the return of the function checks code to a flag. The idea is to make the flag trigger the loop if the code is incorrect

char *leValidaCodigoAviao(char *msg, char *msgErro){
    char *pNome=0, nome[MAX];
    strcpy(nome, "");
    int stringTam=0, flag=1, verificador=0;

    do{
        printf("%s", msg);
        fflush(stdin);
        scanf("%[^\n]s", nome);

        if(strlen(nome)==0){
            printf("%s", msgErro);
            flag=0;
        }else if(strlen(nome)<COD_ID || strlen(nome)>COD_ID){
            flag=0;

        }else{

            verificador = verificaCodigo(nome);
            printf("%d", verificador);
            flag = verificador;
        }


    }while(!flag);
    system("cls");
    getch();
    return pNome = nome;
}
int verificaCodigo(char nome[]){
    int i, flag=1;


    for(i=0;i<COD_ID;i++){
        if(i<3){
            if(!isalpha(nome[i])){
                flag=1;
                break;
            }
        }else{
            if(!isdigit(nome[i])){
                flag=1;
                break;
            }
        }
    }

    return flag;
}
  • It was only supposed to enter the loop if the code was incorrect. For example, ma10340 -> 3143mdafd -> mar103b. Otherwise, it exits the loop. I already changed the values of the flags, but nothing changed.

  • But you must return 0 if you’re wrong and 1 if you’re right, assuming that right is 3 letters followed by 4 numbers is that ?

  • Yes. I tested it here, but for some reason it keeps giving error.

  • I got it. It was a problem with string size. Thanks, Isac! It helped a lot, partner!

1 answer

1


In the way that its function verificaCodigo is written, it is easier to change the assignment you have in ifs to do what you want. So every time you see that a letter or a digit is not in the right place, you put the flag to 0 instead of 1 and ends the for:

int verificaCodigo(char nome[]){
    int i, flag=1;

    for(i=0;i<COD_ID;i++){
        if(i<3){
            if(!isalpha(nome[i])){
                flag=0; //0 em vez de 1
                break;
            }
        }else{
            if(!isdigit(nome[i])){
                flag=0; //0 em vez de 1
                break;
            }
        }
    }

    return flag;
}

Testing:

printf("%d\n", verificaCodigo("ABC1234")); //1
printf("%d\n", verificaCodigo("1BC1234")); //0
printf("%d\n", verificaCodigo("AB11234")); //0
printf("%d\n", verificaCodigo("QQQ9999")); //1
printf("%d\n", verificaCodigo("123ABCD")); //0
printf("%d\n", verificaCodigo("abc1234")); //1
printf("%d\n", verificaCodigo("aBc9999")); //1
printf("%d\n", verificaCodigo("a0b1234")); //0

Check it out at Ideone

The way it started flag in 1 and put 1 within the if it wouldn’t make sense because it was putting the value it already had there, and it didn’t put 0 in no situation.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.