Password testing program

Building a password verification program. I am having difficulty making my logic produce the correct outputs. Very basic code, but really giving me fits, sadly I have a few hours already into this. The out puts are wrong a given code. I believe the " must include a digit" test is functioning well, but testing for an upper case and lower case is definitely incorrect.
Thanks for taking a peak.



#include <cctype>
#include <cstring>
#include <iostream>
using namespace std;



// Function prototype............verify the password.
bool validPassword(char[]);

void main()
{
const int Array_Size = 20;
char password[Array_Size];

cout << "Please enter your password, and I will VERIFY" << endl;
cin.getline(password, Array_Size);
validPassword(password); //call function


system("pause");
}


bool validPassword(char password[])
{

bool upperLower = false;
bool includeNum = false;
int length;



length = strlen(password); //length of string
if (length < 6)
cout << "Invalid Password. Password Should Be Six Characters long!" << endl;

for (int i = 0; i < length; i++)
{
if (isupper(password[i]) || (islower(password[i]))) //isupper,islower found on pg 542 of text.
upperLower = true; //tests each letter for upper and lower case
if (isdigit(password[i])) //step thru, test each character for presence of digit. text pg 542
includeNum = true;
}


if (!upperLower)
cout << "Invalid Password. Please Include At Least One Upper Case And One Lower Case Letter." << endl; // only returns true if both are present


if (!includeNum)
cout << "Invalid Password. Please Include A Number." << endl;

if ((includeNum = true) && (upperLower = true))
cout << "This password passes the verification criteria." << endl;


return 0;


}
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
#include <iostream>
#include <cstring>
#include <cctype>

static bool error( const char* msg ) { std::cerr << "*** error: " << msg << '\n' ; return false ; }

bool valid_password( const char* cstr, bool noisy = true )
{
    static const std::size_t MIN_LENGTH = 6 ;

    const std::size_t nchars = cstr != nullptr ? std::strlen(cstr) : 0 ;
    bool has_lower_case_letter = false ;
    bool has_upper_case_letter = false ;
    bool has_digit = false ;

    for( std::size_t i = 0 ; i < nchars ; ++i )
    {
        if( std::islower( cstr[i] ) ) has_lower_case_letter = true ;
        else if( std::isupper( cstr[i] ) ) has_upper_case_letter = true ;
        else if( std::isdigit( cstr[i] ) ) has_digit = true ;
    }

    if(noisy)
    {
        if( nchars < MIN_LENGTH ) return error( "password must have at least 6 characters" ) ;
        else if( !has_lower_case_letter ) return error( "password must have at least one lower case letter" ) ;
        else if( !has_upper_case_letter ) return error( "password must have at least one upper case letter" ) ;
        else if( !has_digit ) return error( "password must have at least one decimal digit" ) ;
    }

    return nchars >= MIN_LENGTH && has_lower_case_letter && has_upper_case_letter && has_digit  ;
}

int main()
{
    const int SZ = 20 ;
    char password[SZ] ;
    if( std::cin.getline( password, SZ ) && valid_password(password) ) std::cout << "ok.\n" ;
}
Just a couple of things:

Please put your code in code tags.

On the line if( includeNum = true) you are using the assignment operator, it should be ==.

Also, have validPassword(() return the result of your test of that line once you have fixed it.

1
2
3
4
5

bool brc == ( includeNum && upperLower );

return brc;


In main check the return from validPassword() and display your message after.
tried hard to make corrections, am I on the right path? My logic is still corrupt, as I am not getting the warnings I expected with no lowercase or no uppercase.



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
#include <cctype>
#include <cstring>
#include <iostream>
using namespace std;



// Function prototype............verify the password.
bool validPassword(char[]);

bool main()
{
	const int Array_Size = 20;
	char password[Array_Size];
	bool pass=false;
	cout << "Please enter your password, and I will VERIFY" << endl;
	cin.getline(password, Array_Size);
	//if (validPassword(password)==true);					//call function
		//cout << "This password is acceptable!" << endl;
	validPassword(password);
	if (pass)
		cout << "This password is acceptable!" << endl;
	system("pause");
}
	

bool validPassword(char password[])
{
	bool pass = true;
	bool upperLower = false;
	bool includeNum = false;
	int length;
	
	

	length = strlen(password);								 //length  of string
	if (length < 6)
		cout << "Invalid Password. Password Should Be Six Characters long!" << endl;
	
	for (int i = 0; i < length; i++)

	{
		if (isupper(password[i]) || (islower(password[i])))	//isupper,islower found on pg 542 of text.

			upperLower = true;								//tests each letter for upper and lower case
		if (isdigit(password[i]))							//step thru, test each character for presence of digit. text pg 542

			includeNum = true;
	}
	

		if (!upperLower)
			cout << "Invalid Password. Please Include At Least One Upper Case And One Lower Case Letter." << endl;		// only returns true if both are present
	
		
		if (!includeNum)
			cout << "Invalid Password. Please Include A Number." << endl;
		
		if ((includeNum == true) && (upperLower == true))
			return pass=true;
		else
			return 0;

		

}


Thanks for taking a look.
PS thanks for the heads up with regard to code blocks....
Much better!


The function main should return an int so:

 
int main()


You need to add a separate test for lower and upper:

1
2
3
4
5
if( isupper( password[ i ] ) )
    Upper = true;

if( islower( password[ i ] ) )
    Lower = true;


On lines 59 through 62, do it like this:

1
2
3
4
5
6
7
8
9


if( includeNum && Lower && Upper )
    pass = true;
else
    pass = false;

return pass;


In main:

1
2
pass = validPassword( password );
Last edited on
this is great, I added in the length>6 to the return test and it finally corrects the logic.....
Thanks again Kooth!!!!!!!
Anytime, my friend! Please mark this as solved. TTYL!
Topic archived. No new replies allowed.