Password validation help!!!!

I am currently working on a password Validation program. It requires: at least 8 digits long, at lease 1 uppercase letter and 1 lowercase letter, and at least 1 number. The program will give the user only 3 tries. So I wrote the program. When I tested it, it says variable password has not been initialized. Could you please check what went wrong? It seems pretty ok to me... I need this to work ASAP! Thank you very much!

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

using namespace std;

//fuction prototype
bool testPass(char []);

int main()
{
    char *password;

	for(int i = 0; i < 3; i++)
	{
		cout << "Please enter a password that have at least 8 digits " << endl;
		cout <<	"contains at least 1 uppercase letter, " << endl;
		cout << "one lowercase letter, and at least 1 number." << endl;
		cin >> password;
		
		
		if (strlen(password) < 8)
		cout << "invalid password" << endl;

		else
		{
			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;
			}
	
		}
	}

    return 0;
}
//-----------------------------------------------------------------
//This function will check each input and ensure that the password
//contains a uppercase, lowercase, and digit.
//-----------------------------------------------------------------

bool testPass(char pass[])
{
	// 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 ;
}
Last edited on
Your mistake is in reading the password. You can't use a char* like that, beacause you never allocate space for the word.
You could use char password[20]; with a maximum length or allocate the memory at run-time (dynamic allocation) or, maybe better, use a string and read with getline (since a password could also contain one or more spaces).
Use std::string http://www.mochima.com/tutorials/strings.html

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

bool is_valid_password( std::string pword )
{
    // at least 8 characters
    if( pword.size() < 8 ) return false ;

    // at lease 1 uppercase letter and 1 lowercase letter, and at least 1 number
    bool aUpper = false, aLower = false, aDigit = false ;
    
    // http://www.stroustrup.com/C++11FAQ.html#for
    for( char c : pword )
    {
        if( std::isupper(c) ) aUpper = true ;
        else if( std::islower(c) ) aLower = true ;
        else if( std::isdigit(c) ) aDigit = true ;
    }

    return aUpper && aLower && aDigit ;
}

int main()
{
    std::string pword ;
    std::getline( std::cin, pword ) ; // if pass word may contain spaces
    // std::cin >> pword ; // otherwise

    if( is_valid_password(pword) )
    {
        // ...
    }
}
Last edited on
minomic Thank you sooo much!!!!!!!!!!!! I changed the variable to char password[20] and it works perfectly now!!
Good! Please bear in mind that in this way you are limiting the maximum length of the password. It could be ok but it could also be a mistake... It depends on whether you want to allow passwords longer than 20 characters or not. In case you want, the solution with a string would be much more flexible.
I see what you mean...Thanks bro
I changed my maximum to 1000 lol, it's like a cheat way for it~~ :D
Ok, that's another solution. You are wasting some memory but if you don't have particular constraints then it could be negligible.
Topic archived. No new replies allowed.