0

A password is good if it has at least one digit,one alphanumeric charecter, one uppercase letter, one ordinary lowercase letter letter and consists of 8 charecters or more. I created this program, but I'm not getting correct results.. What is the problem?

void main()
{
cout<<"This program checks whether the password entered by the user is good or not"<<endl;
cout<<"Enter the password"<<endl;
char password[100];
int atleastoneDigit=0,atleastoneLowercaseletter=0,atleastoneUppercaseletter=0,atleastonealphanumericCharecter=0;
cin>>password;
int length=strlen(password);
for(int i=0;i<length;i++)
{
if(password[i]>=33&&password[i]<=58)//ASCII values for uppercase letters
atleastoneUppercaseletter++;

else if(password[i]>=65&&password[i]<=90)
        atleastoneLowercaseletter++;
else if(password[i]>=16&&password[i]<=25)
    atleastoneDigit++;
else
atleastonealphanumericCharecter++;

}

    if(length>=8&&atleastoneDigit!=0&&atleastoneUppercaseletter!=0&&atleastoneLowercaseletter!=0&&atleastonealphanumericCharecter!=0)
        cout<<"This is a good password!"<<endl;
    else
        cout<<"This is not a good password!"<<endl;

}

Edited by gethelpcpp123

4
Contributors
3
Replies
4
Views
5 Years
Discussion Span
Last Post by WaltP
0

what if you check it this way..

hasAlpha...hasLower are boolean
pw is string...

for(int i=0; i<pw.length(); i++){
    if(isalpha(pw[i]))
        hasAlpha = true;
    if(isdigit(pw[i]))
        hasDigit=true;
    if(isupper(pw[i]))
        hasUpper =true;
    if(islower(pw[i]))
        hasLower = true;
    }

Edited by Nick Evan: Fixed formatting

0

Check your ascii values.
Digits are decimal 48 to 57.
If the password contains alpha characters (a->z, A->Z)and numeric(0->9) characters it is then refered to as alphanumeric.

Cheers
Milton

0

Expanding on MNeal's post, stop using magic numbers. If you want to test for the character 'A', use 'A', not 65. Zero is '0', not 48.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.