Help with a password validator.

Oct 29, 2015 at 3:12am
In writing my code for this function, the password always returns as failed-- so it's looping infinitely. If I attempt to follow the pwFailure variable in my head, the passwords I'm inputting should work.

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
  void passTest(char desiredPw[], bool pwFailure)
{
	int failCount = 0;
	
	do
	{
		if (failCount == 0 || failCount == 5)
		{
			cout << "\nWhen making a password, be sure that it follows these rules:" << '\n' <<
				"1. Starts with an alphabetic character." << '\n' <<
				"2. There are no spaces or tabs." << '\n' <<
				"3. Contains at least one upper case letter." << '\n' <<
				"4. Contains at least one special character." << '\n' <<
				"5. Contains at least one digit." << '\n' << '\n';

			failCount = 0;
		}

		int hasUpper = 0;
		int hasDigit = 0;
		int hasSpecChar = 0;

		cout << "Now enter your desired password: ";
		cin.get(desiredPw, SIZE, '\n');
		cin.ignore(100, '\n');
		int pwLength = strlen(desiredPw);
		//Ensures the password begins with a letter.
		if (!isalpha(desiredPw[0]))
		{
			pwFailure = true;
		}
		for (int i=0; i<pwLength; ++i)
		{

			//Checks the password for a space or tab character.
			if (isspace(desiredPw[i]))
			{
				pwFailure = true;
			}	
			//Checks the password for an uppercase.
			if (isupper(desiredPw[i]))
			{
				++hasUpper;
			}
			//Checks the password for a digit.
			if (isdigit(desiredPw[i]))
			{
				++hasDigit;
			} 
			//Checks the password for a special character.
			if (!isalnum(desiredPw[i]))
			{
				++hasSpecChar;
			}
			//If the password has none of these, it will fail here.
			if (hasUpper == 0 || hasDigit == 0 || hasSpecChar == 0)
			{
				pwFailure = true;
			}
		}
		//Counts up to five failures and will repeat the rules at 5.
		if (pwFailure == true)
		{
			++failCount;
		}
		
	} while (pwFailure == true);			
}
Oct 29, 2015 at 3:19am
closed account (48T7M4Gy)
There doesn't appear to be any case where pwFailure = false, it's always true.

Don't you need some else's
Last edited on Oct 29, 2015 at 3:20am
Oct 29, 2015 at 3:42am
I have pwFailure declared and set to false in int main(), and then called in through an argument on the passTest function.

Is that not OK?

edit: grammar.
Last edited on Oct 29, 2015 at 4:18am
Oct 29, 2015 at 3:55am
closed account (48T7M4Gy)
Is that not OK?


It is OK in principle. But I want to check something first. I'll get back.
Oct 29, 2015 at 4:09am
closed account (48T7M4Gy)
It's OK so the problem must be with the logic tests further down.
Oct 29, 2015 at 4:26am
I've commented everything else out so that this essentially is the whole program-- it keeps doing the same thing.

Password test input I'm using is "grEEn@5", which shouuuuld work.

I'm thinking it might have something to do with the "isspace" portion.

Here's my main (I took out the other functions [and tested the program without-- same result]):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
const int SIZE = 30;

void passTest (char desiredPw[], bool pwFailure);

int main()
{
	char desiredPw[SIZE];
	bool pwFailure = false;

	cout << "\nWelcome! Let's generate a password-- you can exit the program once you have successfully created a strong password.";

	//Calling the passTest function to determine whether or not the password is usable within our parameters.
	passTest(desiredPw, pwFailure);

	cout << "\nYou have successfully set your password.\n";

return 0;
}

Does the SIZE mess with my isspace?
Oct 29, 2015 at 4:28am
closed account (48T7M4Gy)
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
#include <iostream>

using namespace std;

int main()
{
	char desiredPw[] = "aBc12B ?";
	int pwLength = sizeof(desiredPw) / sizeof(char);
	bool pwFailure = false;

	if (!isalpha(desiredPw[0]))
	{
		pwFailure = true;
		cout << "Password fails - first character not alpha uppercase" << endl;
	}

	for (int i = 0; i < pwLength; ++i)
	{
		//Checks the password for a space or tab character.
		if (isspace(desiredPw[i]))
		{
			pwFailure = true;
			cout << "Password fails - space or tab" << endl;
		}
		/*
		//Checks the password for an uppercase.
		if (!isupper(desiredPw[i]))
		{
			++hasUpper;
		}
		//Checks the password for a digit.
		if (isdigit(desiredPw[i]))
		{
			++hasDigit;
		}
		//Checks the password for a special character.
		if (!isalnum(desiredPw[i]))
		{
			++hasSpecChar;
		}
		//If the password has none of these, it will fail here.
		if (hasUpper == 0 || hasDigit == 0 || hasSpecChar == 0)
		{
			//pwFailure = true;
		}
		*/
	}

	system("pause");

}


I'd suggest you test it progressively using a small test program with diagnostic messages like this one. ( or maybe somebody will jump in ?)

The other thing is you might need to separately loop through the string several times because the tests are mutually exclusive in a couple of cases.
Last edited on Oct 29, 2015 at 4:40am
Oct 29, 2015 at 4:39am
Thank you! I will go ahead and tinker with this for a bit.
Oct 29, 2015 at 4:42am
closed account (48T7M4Gy)
Cheers, but feel free to get back if it all gets too tough.
Oct 29, 2015 at 11:44am
closed account (48T7M4Gy)
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
#include <iostream>

using namespace std;

int main()
{
	int hasUpper = 0, hasDigit = 0, hasSpecChar = 0;
	bool pwFailure = false;
	
	char desiredPw[] = "b1a1A";
	int pwLength = sizeof(desiredPw) / sizeof(char) - 1;


	cout << "Password length = " << pwLength << endl;

	if (!isalpha(desiredPw[0]))
	{
		pwFailure = true;
		cout << "Password fails - first character not alpha uppercase" << endl;
	}

	for (int i = 0; i < pwLength; ++i)
	{
		//Checks the password for a space or tab character.
		if (desiredPw[i] == ' ' || desiredPw[i] == '\t')
		{
			pwFailure = true;
			cout << "Password fails - space or tab" << endl;
		}

		//Checks the password for an uppercase.
		if (isupper(desiredPw[i]))
		{
			++hasUpper;
		}

		//Checks the password for a digit.
		if (isdigit(desiredPw[i]))
		{
			++hasDigit;
		}

		//Checks the password for a special character.
		if ( !(isalnum(desiredPw[i])) )
		{
			++hasSpecChar;
		}
	}

	//If the password has none of these, it will fail here.
	if (hasUpper == 0)
	{
		pwFailure = true;
		cout << "Password fails - no uppercase" << endl;
	}

	if (hasSpecChar == 0)
	{
		pwFailure = true;
		cout << "Password fails - no special character" << endl;
	}

	if (hasDigit == 0)
	{
		pwFailure = true;
		cout << "Password fails - no digit" << endl;
	}

	if (pwFailure == true)
		cout << "Password failed" << endl;
	else
		cout << "Password succeeded" << endl;

	system("pause");
}
Topic archived. No new replies allowed.