Password validation for school

I am only on my second course for C++ and I have an ok understanding on it. My code here is to validate a password to follow the criteria. I am having a problem with though not actually validating the criteria. iT sounds sound in my head, but doesn't computer properly

I have functions set up to count the character and return the value back, then i test that value against == 0. But it never outputs the proper text and always throws up the last entry "It being valid".

I've tried "for" stepping it through the array and that gave me an abort error.

I'd like to just figure out why it is not returning the proper value and skipping through the validations.

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
86
87
88
89
90
#include <iostream>
#include <cctype>
#include <cstring>
using namespace std;

int testUpper(char*);
int testLower(char*);
int testDigit(char*);


int main()
{
	const int size = 20;
	char password[size];
	int length;

	cout << "Please enter your password." << endl << endl;
	cout << "Please remember that it must be at least 6 characters long, " << endl;
	cout << "it must contain at least one uppercase letter and one lowercase letter, " << endl;
	cout << "and it must have at least one digit." << endl << endl;

	cin.getline(password, size);
	length = strlen(password);

	while (length <6)
	{
		cout << "Please enter a password with at least 6 characters." << endl;
		cin.getline(password, size);
		length = strlen(password);
	}

	testUpper(password);
	if (testUpper == 0)
		cout << "You do not have any uppercase characters." << endl;

	testLower(password);
	if (testLower == 0)
		cout << "You do not have any lowercase characters." << endl;

	testDigit(password);
	if (testDigit == 0)
		cout << "You do not have any numerical characters." << endl;
	
	
	if (testUpper != 0 && testLower != 0 && testDigit != 0)
		cout << "Your password is valid." << endl;
	else
	{
		cout << "Your password is not valid." << endl;
	}
	
	getchar();
	return 0;
}

int testUpper(char *strPtr)
{
	int occurs = 0;
	while(*strPtr != '\0')
	{
		if (isupper(*strPtr))
			occurs++;
		strPtr++;
	}
	return occurs;
}

int testLower(char *strPtr)
{
	int occurs = 0;
	while (*strPtr != '\0')
	{
		if (islower(*strPtr))
			occurs++;
		strPtr++;
	}
	return occurs;
}

int testDigit(char *strPtr)
{
	int occurs = 0;
	while (*strPtr != '\0')
	{
		if (isdigit(*strPtr))
			occurs++;
		strPtr++;
	}
	return occurs;
}
The way you're calling your functions is not right.
Your functions return an int.
So you need to create an int and use that to call your functions.
So do something like this:

1
2
3
4
5
6
7
8
9
10
11
12
//Line 32:
int Upper;
Upper = testUpper(password);
    if (Upper == 0)
           cout << "You do not have any uppercase characters." << endl;

// Same thing for lowerCase testing
// line 36
int Lower;
Lower = testLower(password);
   if (Lower == 0)
      cout << "You do not have any lowercase characters." << endl;


Now you get the idea..
This is how you call functions.
The way you are calling your functions is okay. But in order for it to work, your functions need to be void.

Don't forget to do the same thing for testDigit() function.
And your line 45 should use the ints you created and not the name of your functions

Hope that helps!
Last edited on
Thank you so much. This helps. It is working as intended.
Time to hit the books more now.
Topic archived. No new replies allowed.