password verification code review

Hello I coding a password verification in my programming class but when executed it fails to recognize a valid password. The requirements for the password are that it must be at least 6 characters have at least one uppercase and one lower case character as well as having at least one number. I have concerns over how I wrote the prototype function and my for loop inside the isValid function. can anyone look through this code and point me in the right direction?

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

const int SIZE = 80;
const int MIN = 6;

// function prototypes

int isValid(char pwd[]);


int main()
{
	char password[SIZE];
	while (true)
	{
		// conditions
		cout << "Please enter a password that contains" << endl;
		cout << "at least 6 characters" << endl;
		cout << "at least one uppercase and one lowercase character" << endl;
		cout << "and has at least one digit." << endl;
		
		cin.getline(password, SIZE);
		if (isValid(password) == 1)
		{
			cout << "Good Password!";
			break;
		}
		else
		{
			cout << "password was invalid" << endl;
		}
	}
	system("pause");
}
int isValid(char pwd[])
// requirments tests
{
	bool minLength = false;
	bool hasUpper = false;
	bool hasLower = false;
	bool hasDigit = false;
	int length = strlen(pwd);
	if (length >= MIN)
	{
		minLength = true;
	}
	for (int i = 0; i < length; i++)
	{
		if (isupper(*pwd))
			hasUpper = true;
		if (islower(*pwd))
			hasLower = true;
		if (isdigit(*pwd))
			hasDigit = true;
		*pwd ++;
		if (minLength == true && hasUpper == true && hasLower == true && hasDigit == true)
			return 1;
		else
			return 0;
	}
}
Think carefully about line 56. Are you incrementing the pointer ... or the variable it points to?

Then consider where you put your tests (lines 57 to 60). These need to be AFTER the looping, not within it, or they will make their judgement after the first letter.

On a lesser note, you may need
#include <cstring>

A good debugging skill is to print out the values of some variables; e.g. within your loop. Just looking at code won't tell you what is wrong.
Last edited on
Thank you so much, the solution was right under my nose. now i know what to look for in the future
Topic archived. No new replies allowed.