mardi 1 mai 2018

how to use bool, if statements, while statements, used to check the validity of user input

I've been trying to create a password checker with the following code below.

bool alpha = false;
bool digit = false;
bool nogo = true;
char pass[99];
int len;
len = strlen(pass);
int i, x;

while(nogo){

printf("Password checker 1.1\n");

printf("Please enter a password you would like to test.\n");
scanf("%s\n", pass);
printf("%d\n", len);

    if(len<8){
        printf("Your password is too short. Please re-enter a new password.");
        nogo = true;
    }

    if(len>20){
        printf("Your password is too long. Please re-enter a new password.");
        nogo = true;
    }

    else{
        for (int i = 0, len = strlen(pass); i < len; i++){
            if(isalpha(pass[i]) == true){
                alpha = true;
            }
        }
        for (int x = 0, len = strlen(pass); x < len; i++){
            if(isdigit(pass[x]) == true){
                digit = true;
            }
        }
    }

if(alpha == true && digit ==true ){
    printf("Your password is good.");
    nogo = false;
    return 0;
}
}

Everything was built with no errors. However, the code past the first input of the password did not seem to run properly. I had to enter an input twice it prompted for another input of password. Also, it refused to accept the second input from the user. The following is what is shown on my console.

Password checker 1.1
Please enter a password you would like to test.
asdasdada
dsadsadadasdaewqkjlks
6
Your password is too short. Please re-enter a new password.

"asdasdada dsadsadadasdaewqkjlks" ---->they are my inputs and the '6' following my inputs are supposed to be my strlen.

May i ask what is the problem with my code? It was my first time using the bool so may i ask if i used the code the right way?

Aucun commentaire:

Enregistrer un commentaire