Password verification

I am not even getting the error statement back. I am guessing that the issue is in main and not in the other functions. I am unsure of where. The if statement looks valid to me.

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
cin.getline(passWord,20)

if (testPassWord(passWord))
	{
		cout << "Please wait - your password is being verified" << endl;
	}
	else
	{

		cout << "Invalid password. Please enter a password "
			<< "with exactly 5 letters and 3 digits" << endl;
		cout << "For example, my37RuN9 is valid" << endl;
	}

bool testPassWord(char custPass[])
{
	int numLetters, numDigits, length;

	length = strlen(custPass);
	numLetters = countLetters(custPass);
	numDigits = countDigits(custPass);
	if (numLetters == 5 && numDigits == 3 && length == 8)
	{
		return true;
	}
	else
	{
		return false;
	}
}
Last edited on
Your testPassWord function looks fine, as does the call to it.

I would suggest putting a cout of passWord just before you call testpassWord to see what you are passing. Or step through it using a debugger.
It displays what is typed just fine.

I am not sure what you meant by step through it using the debugger?
Last edited on
You could have a problem in your countLetters or countDigits functions. Since you did not provide those, it's hard to say.

I am not sure what you meant by step through it using the debugger?

http://www.cplusplus.com/forum/articles/28767/

Last edited on
I'm still working at this and will update if I find a solution.

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
int countLetters(char *strPtr)
{
	int occurs = 0;
	while (*strPtr != '\0')
	{
		if (isalpha(*strPtr))
		{
			occurs++;
			strPtr++;
		}
	}
	return occurs;
}

int countDigits(char *strPtr)
{
	int occurs = 0;
	while (*strPtr != '\0')
	{
		if (isdigit(*strPtr)) 
		{				  
			occurs++;
			strPtr++;
		}
	}
	return occurs;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
int countLetters(char *strPtr)
{
	int occurs = 0;
	while (*strPtr != '\0')
	{
		if (isalpha(*strPtr))
		{
			occurs++;
			strPtr++;
		}
	}
	return occurs;
}


You need to advance your pointer also when !isalpha(*strPtr)
Same with countDigits

Why don't you use a for loop and strlen() ?
Silly me! Thanks Thomas1965.
And AbstractionAnon for the debugging tips.
Topic archived. No new replies allowed.