Small issue with Challenge #1

+1 Wilson Ly · June 16, 2015
So I'm new to programming and C is my first language.
I've been trying to do the first challenge but I'm getting a logic error.

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

int main()
{
    char password[21];
    int i, upper, number, dollar, lower;
    upper = number = dollar = lower = 0;
    printf("Please enter your password [It can only have a maximum of 20 characters]: ");
    scanf(" %s", password);

    for(i=0;i<=21;i++)
    {
        if(isalpha(password))
            {
                if(isupper(password))
            {
                ++upper;
            }
            else
            {
                ++lower;
            }
            }
        else if(isdigit(password))
        {
            ++number;
        }
        else if(password=='$')
        {
            ++dollar;
        }
    }
    printf("Upper:%d Lower:%d Number:%d Dollar:%d\n", upper, lower, number, dollar);
    if(upper>0 && number >0 && dollar >0)
    {
        printf("Password is strong");
    }
    else if(upper==0)
    {
        printf("You need an upper case letter in your password\n");
    }
    if(number==0)
    {
        printf("You need a number in your password\n");
    }
    if(dollar==0)
    {
        printf("You need a dollar sign in your password\n");
    }
    return 0;
}


So i was running the code multiple times to see if anything was wrong and I noticed that even though I didn't put an upper case letter into my password, the message "You need an upper case letter in your password" wouldn't appear. So i checked the formatting and it looked all good to me. Then I decided to check the 'upper', 'lower', 'number' and 'dollar' values right after the for loop and what do you know, it has given my variable 'upper' an additional value of 1. I've been trying to find where I made that logic error but to no avail :(.

Please help me spot the error! :D
Thanks
Wilson

Post a Reply

Replies

Oldest  Newest  Rating
0 Jiahao Wang · June 28, 2015
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <ctype.h>

int main()
{
int upper,lower,number,dollar=0;
char password[21];
printf("Enter your password\n");
scanf("%s",&password);

for(int i=0; i<21&&password!='\0';i++){
if(isalpha(password)){
if(isupper(password)){
upper++;
}
else{
lower++;
}


}
if(isdigit(password)){
number++;
}
if(password=='$'){
dollar++;
}

}
if(upper==0){
printf("you need at least one upper case!\n");
}
if(lower==0){
printf("you need at least one lower case!\n");
}
if(dollar==0){
printf("you need at least one dollar sign!\n");
}
printf("upper:%d lower:%d number:%d dollarsign:%d\n",upper,lower,number,dollar);
0 Jiahao Wang · June 29, 2015
you have a few issues.
1. remember to use "&password" when you scanf (actually you can use "password" since password is defined as password[ ], sorry to tell the wrong thing)
2. when you want to check the password contains upper case or lower case or $ sign or number, YOU SHOULD NOT USE password, e.g. if i input 123Qw as the password, the computer will check 123Qw is a number or not, well the computer will say "123Qw" is not a number. So what you need to do is make the password become an array(password, and then use a for loop to check, so the computer will loop to see the password contains numbers or not.

Hope you can understand my explain.
  • 1

C

107,270 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator