Password Verifier

Write your question here.
#include "stdafx.h"
#include<iostream>
#include<cstring>
#include<cctype>
using namespace std;
bool testpassword(char[],int);
int _tmain(int argc, _TCHAR* argv[])
{ const int size=20;
char password[size];
int length;
bool pass =false;

do{
cout<<"Enter your password:";
cin>>password;

length=strlen(password);
}while(length<6);

if(length>6){
pass=testpassword(password,length);
}

cin.get();
return 0;
}

bool testpassword(char password[],int length)
{
bool test;
bool upperflag = 0, lowerflag = 0, digitflag = 0;

for (int i= 0; i<length; i++)
{
if (isupper(password[i]))
upperflag = true;
else if (islower(password[i]))
lowerflag = true;
else if (isdigit(password[i]))
digitflag = true;
}
if (!upperflag)
cout << "Invalid Password. Please Include At Least One Upper Case And One Lower Case Letter." << endl;
if(!lowerflag)
cout << "Invalid Password. Please Include At Least One Upper Case And One Lower Case Letter." << endl;
if (!digitflag)
cout << "Invalid Password. Please Include A Number." << endl;


if(upperflag && lowerflag && digitflag)
test=true;
else
test=false;
return test;
}

it asks user to enter a password and it has to be contain a upper letter, a lower letter and a digit.
if not, it will show a message.
i don't to what's wrong with my code. the function isn't working. it doesn't test the password.
Thank you
Please use code tags. http://www.cplusplus.com/articles/jEywvCM9/
You can edit your post, highlight your code and click the <> button on the right.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include "stdafx.h"
#include <iostream>
#include <cstring>
#include <cctype>
using namespace std;

// use underscores or camel case
// so that the function doesn't
// look like areallylongwordlikethis
bool test_password( char[], int );

int _tmain(int argc, _TCHAR* argv[])
{ 
    const int size = 20;
    // initialise variables
    char password[size]{};
    int length{};
    bool pass = false;

    do {
        cout << "Enter your password:";
        cin >> password;

        length = strlen( password );
    } while( length < 6 );

    // what if the password is more than 20 characters?
    
    if( length > 6 )
        pass = test_password( password, length );

    if( pass )
        cout << "passed!\n";
    else cout << "failed!\n";

    cin.get( );
    return 0;
}

bool test_password( char password[], int length )
{
    bool test{};
    bool upperflag = 0, lowerflag = 0, digitflag = 0;

    for ( int i = 0; i < length; i++ ) {
        if( isupper( password[i] ) )
            upperflag = true;
        else if( islower( password[i] ) )
            lowerflag = true;
        else if( isdigit( password[i] ) )
            digitflag = true;
    }

    // prefer to use "\n" over endl
    if( !upperflag )
        cout << "Invalid Password. Please Include At Least One Upper Case Letter.\n";
    if( !lowerflag )
        cout << "Invalid Password. Please Include At Least One Lower Case Letter.\n";
    if( !digitflag )
        cout << "Invalid Password. Please Include A Number.\n";


    if( upperflag && lowerflag && digitflag )
        test = true;
    else test = false;

    return test;
}
@integralfx
1
2
3
4
5
6
7
8
9
10
11
    do {
        cout << "Enter your password:";
        cin >> password;

        length = strlen( password );
    } while( length < 6 );

    // what if the password is more than 20 characters?
    
    if( length > 6)
        pass = test_password( password, length );

The length is already guaranteed to be at least 6 in the while loop. If you do if( length > 6), you are purposefully ignoring checking passwords with length of 6. It is a miserable mistake.

You should change it to :
1
2
3
4
5
6
7
8
    do {
        cout << "Enter your password:";
        cin >> password;

        length = strlen( password );
    } while( length < 6 );

     pass = test_password( password, length );

Topic archived. No new replies allowed.