challenge #1 problem

+1 joey wilmots · December 29, 2014
Hey guys, 

So I have been working on the first challenge and somehow i don't get it to work.
I don't get any errors but the thing is that it is giving me the "your password is good" message, even when the password contains no uppercase letters.

The weird thing is that it works the other way around, so when the password contains at least 1 uppercase letter but no digit I get a message saying "your password needs to contain at least one upper case letter and 1 digit".


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

int main()
{

    char Username[15];
    char Password[18];
    int requirement1 = 0;
    int requirement2 = 0;
    int requirement3 = 0;


    do{

    printf("Username: ");
    scanf(" %s", Username);

    printf("Password: ");
    scanf(" %s", Password);

    if (isalpha(Password[0]) || isalpha(Password[1]) || isalpha(Password[2]) || isalpha(Password[3]) || isalpha(Password[4])
       || isalpha(Password[5]) || isalpha(Password[6]) || isalpha(Password[7]) || isalpha(Password[8]) || isalpha(Password[9])
       || isalpha(Password[10]) || isalpha(Password[11]) || isalpha(Password[12]) || isalpha(Password[13])
       || isalpha(Password[14]) || isalpha(Password[15]) || isalpha(Password[16]) || isalpha(Password[17]) || isalpha(Password[18])
       ){

       if(isupper(Password[0]) || isupper(Password[1]) || isupper(Password[2]) || isupper(Password[3]) || isupper(Password[4])
       || isupper(Password[5]) || isupper(Password[6]) || isupper(Password[7]) || isupper(Password[8]) || isupper(Password[9])
       || isupper(Password[10]) || isupper(Password[11]) || isupper(Password[12]) || isupper(Password[13])
       || isupper(Password[14]) || isupper(Password[15]) || isupper(Password[16]) || isupper(Password[17]) || isupper(Password[18])
       ){

           requirement1++;


    }


    }




    if(isdigit(Password[0]) || isdigit(Password[1]) || isdigit(Password[2]) || isdigit(Password[3])
               || isdigit(Password[4]) || isdigit(Password[5]) || isdigit(Password[6]) || isdigit(Password[7])
               || isdigit(Password[8]) || isdigit(Password[9]) || isdigit(Password[10]) || isdigit(Password[11])
               || isdigit(Password[12]) || isdigit(Password[13]) || isdigit(Password[14]) || isdigit(Password[15])
               || isdigit(Password[16]) || isdigit(Password[17]) || isdigit(Password[18])){

                   requirement2++;


            }


    if ((requirement1>0) && (requirement2>0)){

        printf("your password is good");
    }else if((requirement1<1) || (requirement2<1)){

        printf("your password needs to contain at least one upper case letter and 1 digit\n\n");
    }

    }while((requirement1<1) || (requirement2<1));







    return 0;
}


sorry for the weird layout it's because I let the program check every character on it's own for uppercase or digit.

I hope someone can help me out with this, thanks alot in advance.

(btw, please don't mind if I make any mistakes, English isn't my native language.)

Post a Reply

Replies

Oldest  Newest  Rating
0 c student · December 30, 2014
your password array is declared with size 0, you can't reallocate memory within the program's stack like that because program stacks are made as big as they are required, no more, no less, so you can't add on extra memory.
0 joey wilmots · December 30, 2014
hi guys, 

thanks for the help so far already. I completely changed my program using a for loop for checking for uppercase and digits.
the only problem now is it always gives me the message saying that my password isn't good and it isn't increasing my requirements + 1 either. Can you please help me once more? Thanks alot


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

int main()
{

    unsigned long x = 0;
    char Username[15];
    char Password[x];
    int requirement1 = 0;
    int requirement2 = 0;

    int i;



    do{

    printf("Username: ");
    scanf(" %s", Username);

    printf("Password: ");
    scanf(" %s", Password);

    x = strlen(Password);


    for (i=0; i==x; i++){

            if ((isalpha(Password)) && (isupper(Password))){

                        requirement1++;


                    }


                if(isdigit(Password)){

                requirement2++;
            }


            }





    if((requirement1>=1) && (requirement2>=1)){

        printf("your password is good\n\n");
        printf(" %d, %d\n\n", requirement1, requirement2);

    }else{

        printf("your password needs to contain at least one uppercase letter and one digit\n\n");
        printf(" %d, %d\n\n", requirement1, requirement2);


    }


    }while((requirement1<1) || (requirement2<1));







    return 0;
}
0 Sachin Gupta · December 29, 2014
please try removing "Password[18]" condition from all the checks and post the results 

Regards,
Sachin
0 Stefan Gost · December 29, 2014
well a better way to check every char in an array would be with a loop
this will make it easier to review the code and it will prevent you from making typos or other mistakes
  • 1

C

106,942 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator