Password Verifier

Hello,

The only error I am getting is when the password entered is longer than 6 characters. I have tried a few things here and there but i cant seem to crack it. Any help would be greatly appreciated.


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

using namespace std;
bool testPassword(char pass[], const int SIZE);

int main()
{
	const int SIZE = 7;
	char pass[100], password[SIZE];
	int count = 0;

	cout << "Enter a password with at least " << (SIZE - 1) << " characters." << endl;
	cout << "The password must contain a numerical digit, a lowercase letter," << endl;
	cout << "and an uppercase letter." << endl;
	cin.getline(pass, 100);

	if (testPassword(pass, SIZE))
	{
		cout << "The password you entered: ";
		strncpy_s(password, pass, SIZE);
		while (password[count] != '\0')
		{
			cout << password[count];
			count++;
		}
		cout << " is valid!" << endl;
	}

	system("pause");
	return 0;
}

bool testPassword(char pass[], const int SIZE)
{
	bool passUpper = false;
	bool passLower = false;
	bool passDigit = false;
	int length = strlen(pass);

	for (int i = 0; i < length; i++)
	{
		if (isdigit(pass[i]))
			passDigit = true;
		if (islower(pass[i]))
			passLower = true;
		if (isupper(pass[i]))
			passUpper = true;
	}
	if (length < (SIZE - 1))
		cout << "Error. Your password does not meet the requirement of " << (SIZE - 1) << " or more characters." << endl;
	if (passDigit == false)
		cout << "Error. Your password does not contain a numerical digit." << endl;
	if (passLower == false)
		cout << "Error: Your password does not contain a lowercase letter." << endl;
	if (passUpper == false)
		cout << "Error: Your password does not contain an uppercase letter." << endl;

	if (length < (SIZE - 1) || passDigit == false || passLower == false || passUpper == false)
	{
		cout << "Please try again with the instructions above." << endl;
		return false;
	}
	else
		return true;
}
Last edited on
closed account (48T7M4Gy)
line 23 try strncpy(password, pass, SIZE);

It worked for me. It truncates any over size which is a bit unusual but ...


Enter a password with at least 6 characters.
The password must contain a numerical digit, a lowercase letter,
and an uppercase letter.
1234AAdi
The password you entered: 1234AAd is valid!
Last edited on
The problem I can see with your code are lines 11 and 12. You are making the password that they can enter to be limited to 6 characters because there is only space in your character array for 7 elements including the terminator character. So you need to tweak the code a little bit to make the 2 arrays match in size otherwise there are more characters that can fit in the array.

Unless you have to avoid using the string container I would advise that you use the C++ string container for your code instead of raw character arrays and C methods. It makes things a lot easier for you and it also means you can have as many characters as you need without having to do any allocation yourself.

http://www.cplusplus.com/reference/string/string/
Sorry for the late response everyone, Ive been slammed with work and other classes. Thanks for your advice, it woke up the sleeping portion of my brain to find a solution. Thanks again!
Topic archived. No new replies allowed.