Need Help With Challenge #1

0 Roshni Merugu · July 8, 2015

#include
#include


int main()
{

    char password;

    printf("Enter a password.\nIt must have a capital letter and a number.\n");
    scanf("%s", &password);

    if( isupper(password) && isdigit(password) )
    {
       printf("You're password is strong");
    }
    else
    {
        printf("You're password is not strong enough.");
        main();
    }

    return 0;
}

Hey everyone. I'm still kind of a beginner at C. I don't understand what is going wrong. No matter what I type in, it goes straight to the else code. Can you guys help me out?

This is Challenge #1

Post a Reply

Replies

Oldest  Newest  Rating
0 Jeff T. · July 13, 2015
There are a few things wrong with the code. But to answer the question asked, your variable 'password' needs to be an array. A char variable only stores one character. So I could enter 123XyZ but since char variable only stores one character, it stores the first character entered, a '1' in my example, so now the 'if' statement is always going to fail and jump to the else statement.
Look at some of the other posts and their answers to get a better idea of what the code should look like. And since your just starting out take an hour or so and learn how to use the debugger, it's an invaluable tool especially when your just starting. It will show you line by line what the computer is doing, which results in understanding the code better and making it easier to write. There are plenty of videos on how to debug. Even use this code as an example in the debugger it will show you what is stored in your password variable and how/ why it jumps to the else statement.  Debugging really isn't that hard or hard to learn. Especially now because your codes will be pretty small and simple but you will be able to spot errors very quickly.
Hope this helps and if I didn't mention it, learn the debugger(ha). Good Luck!
  • 1

C

107,002 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator