简体   繁体   中英

loop repeating printf but not scanf

I made a simple (and ugly) program to check the password that user inputs for uppercase, lowercase, number and the dollar sign. the problem is that my printf in the beginning of my do while loop repeats itself awkwardly while the scanf doesn't even stop it, and that repetition depends on the input of the user. i put some comments to explain the code, and i'll do some screen shots to show you what i mean by printf repeating itself.

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#include<ctype.h>

int main(){
char pass[10];
int i,t,x,y,z;
t=y=z=0;

do{
    printf("Enter a password it needs to have an uppercase letter a lowercase one, a number and a $ sign: ");
    scanf(" %c", pass);



    //here i test for upper case, letter by letter
    for(i=0;i<10;i++){
        if(isalpha(pass[i])){
            if(isupper(pass[i])){
                t++;
            }
        }
    }

    //here i test for lower case, letter by letter
    for(i=0;i<10;i++){
        if(isalpha(pass[i])){
            if(islower(pass[i])){
                y++;
            }
        }
    }

    //here i test for number, letter by letter
    for(i=0;i<10;i++){
        if(isdigit(pass[i])){
            z++;
        }
    }

    //here i look for the dollar sign
    for(i=0;i<10;i++){
        if(pass[i]=='$'){
            x++;
        }
    }

    printf("\n");

    //if all the tests were true the variables t,x,y and z would not be 0
}while(t==0 || x==0 || y==0 || z==0);

}

the first display of the code works fine: enter image description here

however as you will see in this picture, when i enter a bad password the printf repeats: enter image description here

and the repeating of printf depends on how much the user enters: enter image description here

even when i enter a good password, the printf repeats itself a bunch of times before the code ends: enter image description here

i assume the problem is with the for loops in bedded in the do while loop, but i just can't find where exactly is the problem and why printf repeats so weirdly

The main issue is with:

scanf(" %c", pass);

This tries to scan in a single char. To scan in a string use:

scanf("%9s", pass);

The 9 puts a limit on how many chars can be inputted to avoid overflow. 10 is not used because you need to leave room for the end-of-string marker ('\\0').

Some other improvements:

do{
    // Move here to reset each time
    t = y = z =0;

    printf("Enter a password it needs to have an uppercase letter a lowercase one, a number and a $ sign: ");

    // Should always test the result of scanf
    if (1 != scanf("%9s", pass)) {
        // Some error
        break;
    }

    // Combine all the for loops
    for (i = 0; pass[i]; i++) {
        if (isupper(pass[i])) {
            t++;
        }
        else if (islower(pass[i])) {
            y++;
        }
        else if (isdigit(pass[i])) {
            z++;
        }
        else if (pass[i] == '$'){
            x++;
        }
    }

    printf("\n");

    //if all the tests were true the variables t,x,y and z would not be 0
} while (t==0 || x==0 || y==0 || z==0);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM