Validating Password

This is a homework assignment that I'm trying to get a little direction on. The assignment states that I'm to accept a password from a user that is a minimum of 6 characters long contains at least 1 upper case letter, 1 lower case letter and 1 number.

I don't know how to get the while statement in line 71 to allow me to use the isupper, islower, isdigit functions. The textbook wrote a similar program but the password was limited to 7 characters only and the first 3 had to be letters and the last 4 had to be numbers. The while statement will I believe let me find a minimum of one instance of any of these characters and if not then I can have it return a false statement from the bool function. Thank you in advance for the assistance.

/*This program will allow a user to enter a password and check whether or not it is valid.
*/

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

//fuction proto to check pass
bool testPass(char [], int);
int main()
{
char *password; //dynamically allocating an array
int length; //assure requested length and pass length are the same
int numCharacters; //hold number of characters for password

//get the password length from the user
cout << "Please enter how many characters you would like your\npassword to be.";
cout << " Your password must be at least 6 characters long." << endl;
cin >> numCharacters;

//validate
while (numCharacters < 6)
{
cout << "Please enter a password length of at least 6 characters." << endl;
cin >> numCharacters;
}

//dynamically allocate the array for the password
password = new char[numCharacters];


cout << "Please enter a password that contains at least one uppercase letter, ";
cout << "one\nlowercase letter, and at least one digit." << endl;

//get users password
cin >> password;

//convert pointer/array length to interger
length = strlen(password);


//check pointer/array length against user requested pointer/array size
//to ensure consistent data
while (length != numCharacters)
{
cout << "Your password is not the size you requested. ";
cout << "Please re-enter your password." << endl;
cin >> password;
length = strlen(password);
}

if (testPass(password, numCharacters))
cout << "Your password is valid." << endl;
else
{
cout << "Your password is not valid. ";
cout << "Please refer to the above warning message." << endl;
}
return 0;
}

/*This function will check each input and ensure that the password
contains a uppercase, lowercase, and digit.*/

bool testPass(char pass[], int numCharacters)
{
int count = 0; //loop counter

while (pass[count] != islower && pass[count] != '\0')
count++;

if (!islower(pass[count]))
return false;

return true;
}
I don't know how to get the while statement in line 71


Please edit your post and put [code ] - tags around your source code, so we don't have to count by ourselfs. Thanks. (Select your source code and press the small button with "<>" on the right of your text edit box)

Ciao, Imi.
Last edited on
Hey,

I corrected your code a bit (:

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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*This program will allow a user to enter a password and check whether or not it is valid.
*/

#include <iostream>
#include <cstring> // for std::strlen
#include <cctype>
using namespace std;

//fuction proto to check pass
bool testPass(char []); // don't need the 2nd parameter
int main()
{
    char *password; //dynamically allocating an array
    int length; //assure requested length and pass length are the same
    int numCharacters; //hold number of characters for password

//get the password length from the user
    cout << "Please enter how many characters you would like your\npassword to be.";
    cout << " Your password must be at least 6 characters long." << endl;
    cin >> numCharacters;

//validate
    while (numCharacters < 6)
    {
        cout << "Please enter a password length of at least 6 characters." << endl;
        cin >> numCharacters;
    }

//dynamically allocate the array for the password
    password = new char[numCharacters+1]; /// every cstring must end with a '\0' else -> crash


    cout << "Please enter a password that contains at least one uppercase letter, ";
    cout << "one\nlowercase letter, and at least one digit." << endl;

//get users password
    cin >> password;

//convert pointer/array length to interger
    length = strlen(password);


//check pointer/array length against user requested pointer/array size
//to ensure consistent data
    while (length != numCharacters)
    {
        cout << "Your password is not the size you requested. ";
        cout << "Please re-enter your password." << endl;
        cin >> password;
        length = strlen(password);
    }

    if (testPass(password))
        cout << "Your password is valid." << endl;
    else
    {
        cout << "Your password is not valid. ";
        cout << "Please refer to the above warning message." << endl;
    }

    delete[] password ; // hey!! don't forget to free your allocated memory!!!
    return 0;
}

/*This function will check each input and ensure that the password
contains a uppercase, lowercase, and digit.*/

bool testPass(char pass[]) // a beautiful hack (:
{
	// flags
	bool aUpper = false,
		 aLower = false,
		 aDigit = false ;
	for ( int i = 0 ; pass[i] ; ++i )
		if ( isupper(pass[i]) )
			aUpper = true ;
		else if ( islower(pass[i]) )
			aLower = true ;
		else if ( isdigit(pass[i]) )
			aDigit = true ;
	if ( aUpper && aLower && aDigit )
		return true;
	else
		return false ;
}


bye and have fun (:


Last edited on
Thank you MorningStar that was a tight piece of coding. Even better I think I actually may have learned something in the process.
Topic archived. No new replies allowed.