Limit number of digits in data entry

Asked

Viewed 281 times

-2

I’m having a hard time understanding the issue of limiting characters, I researched, but I didn’t understand very well how to use in the code, I made the sketch here, to understand more or less what I want, I know it’s not right if(tamanho<=4), I did just to get the idea, because I want the user to type only 4 digits or less, could give this strength?

int main(){
    int tamanho, cargo;

    printf("1-operador");
    printf("2-operador");

    printf("Digite o numero do cargo");        

    switch(cargo){
        case 1:
            printf("Digite seu codigo de acesso");
            scanf("%d", &codigo);
            if(tamanho<=4){
                if(codigo==1234){
                    printf("joao carlos");
                }
            }
    }

}
  • 1

    If the variable is of the whole type then if (num < 10000) the variable num has a maximum of 4 significant digits.

1 answer

2


There’s a conceptual error there. You’re asking for a password, a descriptive information, so it must be a string and not a number. You could even validate to only have numeric digits, but just because these are accepted characters should not use a numeric type. Numeric types are used to do math, not what you want there.

If you still insist on the error, if you are asking the person to enter the password and check if it is correct, then you do not need to check the size, if the person enters the password with wrong size the password is certainly wrong, the size is not relevant. If you enter the right password then the size will be right.

This would make sense if you were asking the person to enter a password that they would use from there, then you do a validation. Reinforcement that validation should not be so simple and that it should be like a string, but if you insist on the number and need to do a validation (in general password should accept everything) then it should be if the value is less than 10000, because it will certainly have 4 digits (9999) or less, which is strange, I’ve never seen a password be validated to be smaller than a size, in general validates if it has a minimum size, which once again shows how bad it is to use a number. But note that I’m only talking to completeza of reasoning, does not serve for this your code, it does not need to make this validation.

I didn’t even get into the merit of putting a password in the code, because I understand that this is circumstantial for an exercise that matters other things.

  • Oh yes, I got it, I’m new to programming, thanks for the tip, I got it now.

Browser other questions tagged

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