Challenge 1 Help!!H

0 Jojo Kim · September 20, 2014
Hi, I've challenged myself to try the challenge #1 but there seems to be a problem. If you haven't seen the video, challenge #1 for C is to make a program where it checks a password to see if it has an uppercase letter, a digit and a $ sign. I can make it detect the uppercase letter and the $ sign but I can't seem to make it detect the digit. Please help! I've been staring at this code for days!

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

int main()
{




    char password[17];

    int A,B,C,D;

    bool a,b,c,d;


    printf("Please enter a new password. Your password must include an uppercase \nletter, a number and a dollar sign. (Less than 14 characters)\n");
    scanf("%s", password);

    for(A = 0; A <= 0; A++){
        if(isalpha(password[A])){
            a = true;
            break;
        }else{
            a = false;
        }
    }

    for(B = 0; B <= 15; B++){
        if(isupper(password[B])){
            b = true;
            break;
        }else{
            b = false;
        }
    }
    for(C = 0; C <= 15; C++){
        if(isdigit(password[C])){
            c = true;
            break;
        }else{
            c = false;
        }
    }

    for(D = 0; D <= 15; D++){
        if(password[D] == '$'){
            d = true;
            break;
        }else{
            d = false;
        }
    }

    if(a & b & c & d){
        printf("Gratz! You're password is good to go.");
    }else{
        printf("Your password needs to be redone. Read the rules");
    }

 
    return 0;
    }


Post a Reply

Replies

Oldest  Newest  Rating
0 Franz Schmidt · September 20, 2014
Don't know what your problem is?
Code works fine for me...

but you could modify your code a little bit:
You don't need to make a extra loop for every single argument you want to check. Just use one loop and use if and else if statements.

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

int main()
{
char password[14] = { '\0' };
bool a = false, b = false, c = false, d = false;

printf("Please enter a new password. Your password must include an uppercase \nletter, a number and a dollar sign. (Less than 14 characters)\n");
scanf_s(" %s", password, sizeof(password));
if (isalpha(password[0])){
a = true;
for (int i = 0; i < strlen(password); i++){
if (isupper(password)){
b = true;
}
else if (isdigit(password)){
c = true;
}
else if (password == '$'){
d = true;
}
}
}
if (a & b & c & d & strlen(password) < 14){
printf("Gratz! You're password is good to go.\n\n");
}
else{
printf("Your password needs to be redone. Read the rules\n\n");
}
system("pause");
return 0;
}

Btw: You forgot to check if the password is shorter than 14 signs.

Tip: Always give a new created variable a value.
       You don't need to initialize a extra value for a for-loop you can do:
for (int i = 0; i < strlen(password); i++)


       And always make such programs independent of the length of the string, cause if you want to change the passwordlength you don't need to check your whole code.
Don't know which IDE you use, but I had to use scanf_s instead of scanf. (Compiler won't accept scanf anymore.)
0 Franz Schmidt · September 20, 2014
Yes but I can't run my program with scanf it won't let me compile it without change it to scanf_s.
0 Franz Schmidt · September 20, 2014
Worked :O
Didn't know that, thanks man.:D
  • 1

C

107,095 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator