Help with creating a password?

Okay so the following program is supposed to decide whether or not a password is correct give that it is;
-No longer than 16 characters
-No shorter than 8 characters.
It contains at least:
-One uppercase letter
-One lowercase letter
-One number
and
-No special characters //(isalnum(name.at()))

My program will label all passwords as incorrect no matter what, it's breaking my C++ beginning heart </3 :(. Please #include<help> //Hashtag include help thanks.
-P.S. I know I don't need a lot returns true and false but it's just to get used to it for now.

#include<iostream>
#include<cstdlib>
#include<string>
using namespace std;

bool isProper(string);

int main()
{
string password;
cout << "Enter a password that contains:"
<< "-No special characters.\n "
<< "-At least 1 upper, 1 lower case letter and 1 number \n"
<< "Between 8 and 16 characters long. \n";
cin >> password;
while(!isProper(password))
{
cout << "Enter a password: ";
cin >> password;
}

cout << "Your password is " << password << endl;

system("PAUSE");
return 0;
}

bool isProper(string password)
{
if((password.length() < 8)||(password.length() > 16))
return false;

for(int i = 0; i < password.length(); i++)
{
if(isdigit(password.at(i)))
return true;
else
return false;
}

for(int i = 0; i < password.length(); i++)
{
if(isdigit(password.at(i)))
return true;
else
return false;
}

for(int i = 0; i < password.length(); i++)
{
if((isdigit(password.at(i)))&&(islower(password.at(i))))
return true;
else
return false;
}

for(int i = 0; i < password.length(); i++)
{
if(!isalnum(password.at(i)))
return true;
else
return false;
}

return true;
}


Your isProper function isn't doing what you think it's doing. I will say that it's returning prematurely. Can you figure out what's wrong by looking at the following example?

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
#include <iostream>
#include <string>

bool is_proper_string(std::string string) {

	//a string is considered "proper" if it only contains letters (alpha).
	//No digits allowed!

	for (int i = 0; i < string.size(); ++i) {

		if (isalpha(string[i])) {
			return true;
		}
		else {
			return false;
		}

	}

	return false;

}

int main() {

	std::string string = "hello123";

	if (is_proper_string(string)) {
		std::cout << "Proper" << std::endl;
	}
	else {
		std::cout << "Not Proper" << std::endl;
	}

	std::cin.get();
	return 0;
}


The string "hello123" is supposed to be improper, because it contains digits, but this program prints "Proper". What could it be?
@Whiss00, i think your teacher wants you to train more on isupper(), islower(), isalnum()
http://www.cplusplus.com/reference/cctype/

I would prefer to break the program to check every part so that the user can get a friendly error message.

Working code

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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// program to test a valid password
// created by blongho, 2017-05-16
/*
Conditions
- No longer than 16 characters
- No shorter than 8 characters.
It contains at least :
- One uppercase letter
- One lowercase letter
- One number
and
-No special characters 
*/

#include <iostream>
#include <string>
using namespace std;

// is using find_first_of()
//const string LOWERCASE = "abcdefghijklmnopqrstuvwxyz";
//const string UPPERCASE = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
//const string NUMBERS = "0123456789";

bool isShorter(const string password);		// check lower limit
bool isLonger(const string password);		// check upper limit
bool hasUpperCase(const string password);	  // checks if it has uppercase
bool hasLowerCase(const string password);	// checks if it has lowercase
bool hasNumber(const string password);		// checks that password has a number
bool hasSpecialCharacters(const string password); // checks that it has no special characters
bool checkPassword(const string &password);    // checks all conditions and prints result
void start();  // has all the magic

int main()
{
	start();

	return 0;
}
bool isShorter(const string password)
{
	return password.size() < 8 ? true : false;
	// same as
	/*if (password.size() < 8) // password.size() is same as password.lenght();
	{
	return true;
	}
	return false;*/
}

bool isLonger(const string password)
{
	return password.size() > 16 ? true : false;
}

bool hasUpperCase(const string password)
{
	/*size_t found = password.find_first_of(UPPERCASE);
	if (found != string::npos)
	return true;*/
	// check each character in password
	for (auto &c : password)
	{
		if (isupper(c))
			return true;
	}
	return false;
}

bool hasLowerCase(const string password)
{
	/*size_t found = password.find_first_of(LOWERCASE);
	if (found != string::npos)
	return true;*/

	// check each character in password
	for (auto &c : password)
	{
		if (islower(c))
			return true;
	}
	return false;
}

bool hasNumber(const string password)
{
	/*size_t found = password.find_first_of(NUMBERS);
	if (found != string::npos)
	return true;*/
	// check each character in password
	for (auto &c : password)
	{
		if (isdigit(c))
			return true;
	}
	return false;
}

bool hasSpecialCharacters(const string password)
{
	// check each character in password
	for (auto &c : password)
	{
		if (!isalnum(c))
			return true;
	}
	return false;
}

// check validity of password
bool checkPassword(const string & password)
{
	bool isGood = true;

	if (!isShorter(password) && !isLonger(password) && hasUpperCase(password) 
		&& hasLowerCase(password) && hasNumber(password) && !hasSpecialCharacters(password))
	{
		cout << endl << "Proper password." << endl << endl;
		isGood = true;
	}
	else
	{
		cout << endl << "Improper password! Reason..." << endl;

		if (isShorter(password))
			cout << "Your password is shorter than required (at least 8 characters)." << endl;
		if (isLonger(password))
			cout << "Your password can not be longer than 16 characters." << endl;
		if (!hasUpperCase(password))
			cout << "Your password must have at least one upper case letter." << endl;
		if (!hasLowerCase(password))
			cout << "Your password must have at least one lower case letter." << endl;
		if (!hasNumber(password))
			cout << "Your password must have at least one number." << endl;
		if (hasSpecialCharacters(password))
			cout << "No special characters permitted." << endl;
		isGood = false;
	}
	return isGood;
}

// program starts and ends here
void start()
{
	cout << endl << "Hello this program takes and validates your password" << endl << endl;
	string password;
	
	cout << endl << "Please enter your password: ";
	getline(cin, password);

	// continue asking for password as long as it does not satisfy condition
	while (!checkPassword(password))
	{
		cout << endl << "Try another password: ";
		getline(cin, password);
	}
	
}


http://cpp.sh/4bf2k
Last edited on
Hello Whiss00,

Based on what you started with this is what I came up with:

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
#include<iostream>
#include<cstdlib>  // <--- Not sure if this is needed.
#include<string>

bool isProper(std::string);

int main()
{
	std::string password{ "" };
	bool good{ false };

	//  Changed the way this is displayed.
	std::cout << "Enter a password that contains:"
		<< "\n-No special characters.\n"
		<< "-At least 1 upper case letter,\n1 lower case letter and \n1 number \n"
		<< "Between 8 and 16 characters long. \n";

	// Not needed because of change to while loop.
	//std::cout << "\nEnter Password: ";

	//std::cin >> password;

	while (!good)
	{
		std::cout << "\n Enter a password: ";
		std::cin >> password;
		good = isProper(password);
	}

	if (good)
		std::cout << "\n Your password is valid." << std::endl;

	std::cout << "\n Your password is " << password << std::endl;
	
	std::cin.get();
	return 0;
}

bool isProper(std::string password)
{
	bool digit{ false };
	bool uppr{ false };
	bool lowr{ false };

	int numDigits{ 0 };
	int numUppr{ 0 };
	int numLowr(0);

	if ((password.length() < 8) || (password.length() > 16))
		return false;

	for (int i = 0; i < password.length(); i++)
	{
		if (isdigit(password.at(i)))
		{
			digit = true;  // <--- Set not return.
			numDigits++;  // <--- May not be used or used later. Just an idea for now.
		}
	}

	if (!digit)
		std::cout << "\n Your password does not contain a number.\n";

	// Other checks here.

	return (digit && lowr && uppr);
}


Hope this helps,

Andy

Edit changed line 35.
Last edited on
Topic archived. No new replies allowed.