0

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

9
  • 3
    scanf(" %c", pass); is asking for a char. %s is the code for a string. Also, put a size limit on it scanf("%9s", pass); ('9' because you have to leave space for end-of-string char '\0') Commented Jun 30, 2018 at 13:10
  • for(i=0;i<10;i++){ What if the user inputted string is less than 10 chars? Use strlen() instead. Commented Jun 30, 2018 at 13:11
  • scanf(" %c", pass) will read one character by one. So it will repeat printf for each car. Use scanf("%s", pass) to read a string. Commented Jun 30, 2018 at 13:11
  • Thank you so much guys, i don't know why i put %c there... to use strlen() in my for, is it like this ? for(i=0;i<strlen(pass);i++){ Commented Jun 30, 2018 at 13:16
  • Yes. You could also do for (i=0; pass[i]; i++) {. Commented Jun 30, 2018 at 13:19

1 Answer 1

0

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);
Sign up to request clarification or add additional context in comments.

1 Comment

You could drop the isalpha() tests without losing anything of value. A password system that only allows 9 characters where at least one of the characters is a $ isn't all that enticing. However, that's slightly out of scope of the question, and the OP's code probably isn't going to be used for real.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.