coding help

Mar 1, 2016 at 12:47am
So my code doesn't seem to be working for some reason. This code that I am trying to do asks the user to input a password of their choice with the criteria of 1 uppercase, 1 lowercase, 1 number, and 1 symbol. Please help, because I don't know what is wrong with it. It says there are build errors somewhere.

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

int main() {
string password1, password2;

do {
cout << "Please enter your enterprise password: " << endl;
getline(cin, password1);

cout << "Confirm password: " << endl;
getline(cin, password2);

// to compare strings in c++ use "=="
if (password1 == password2)
cout << "Passwords do not match." << endl;
} while (password1 != password2);

// if the length of the password is less than 8, does not meet requirement
if (password1.length < 8) {
cout << "Password too short. Must be at least 8 characters." << endl;
return 0;
}

cout << "Passwords match." << endl;

bool hasUpperCase = false;
bool hasLowerCase = false;
bool hasNumber = false;
bool hasSymbol = false;

for (int i = 0; i < password1.length; i++) {
char c = password1.at(i);

// to determine if c is uppercase??
if (c >= 65 && c <= 90)
hasUpperCase = true;
if (c >= 97 && c <= 122)
hasLowerCase = true;
if (c >= 48 && c <= 57)
hasNumber = true;
if (c >= 33 && c <= 47 && c >= 58 && c <= 64 && c >= 91
&& c <= 96 && c >= 123 && c <= 126)
hasSymbol = true;
}

if (hasNumber == false || hasLowerCase == false || hasNumber == false
|| hasSymbol == false) {
cout << "The \"CS150ROCKS\" password does not meet the minimum requirements of NIST.\n"
<< "Please add 1 or more of the following criteria:" << endl;

if (hasUpperCase = false) {
cout << "At least one upper case letter." << endl;
}
if (hasLowerCase = false) {
cout << "At least one lower case letter." << endl;
}
if (hasNumber = false) {
cout << "At least one number." << endl;
}
if (hasSymbol = false) {
cout << "At least one symbol." << endl;
}
}

else {
cout << "Password has met all the criteria." << endl;
}

system("pause");
return 0;
}
Mar 1, 2016 at 3:48am
It would be helpful if you would do two things. One, put code tags around you code. Two, include the error messages you're getting from the compiler.

Right off the bat, anywhere you have password1.length it needs to be password1.length()
Mar 1, 2016 at 8:27am
Okay, so it actually did something. There is one thing off about this and it's the part where I tell the user that they are missing 1 or more criteria that were listed.
Here's what the code looks like now:

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

int main() {
	string password1, password2;

	do {
		cout << "Please enter your enterprise password: " << endl;
		getline(cin, password1);

		cout << "Confirm password: " << endl;
		getline(cin, password2);

		// to compare strings in c++ use "=="
		if (password1 != password2)
			cout << "Passwords do not match." << endl;
	} while (password1 != password2);

	// if the length of the password is less than 8, does not meet requirement
	if (password1.length() < 8) {
		cout << "Password too short. Must be at least 8 characters." << endl;
		return 0;
	}

	cout << "Passwords match." << endl;

	bool hasUpperCase = false;
	bool hasLowerCase = false;
	bool hasNumber = false;
	bool hasSymbol = false;

	for (int i = 0; i < password1.length(); i++) {
		char c = password1.at(i);

		// to determine if c is uppercase??
		if (c >= 65 && c <= 90)
			hasUpperCase = true;
		else if (c >= 97 && c <= 122)
			hasLowerCase = true;
		else if (c >= 48 && c <= 57)
			hasNumber = true;
		else
			hasSymbol = true;
	}

	if (hasNumber != true || hasLowerCase != true || hasNumber != true
		|| hasSymbol != true) {
		cout << "The \"" << password1 << "\" password does not meet the minimum requirements of NIST.\n"
			<< "Please add 1 or more of the following criteria:" << endl;

		if (hasUpperCase != true) {
			cout << "At least one upper case letter." << endl;
		}
		else if (hasLowerCase != true) {
			cout << "At least one lower case letter." << endl;
		}
		else if (hasNumber != true) {
			cout << "At least one number." << endl;
		}
		else {
			cout << "At least one symbol." << endl;
		}
	}

	else {
		cout << "Password has met all the criteria." << endl;
	}

	system("pause");
	return 0;
}


and this is what I get as the output:


Please enter your enterprise password:
CS150ROCKS
Confirm password:
CS150ROCKS
Passwords match.
The "CS150ROCKS" password does not meet the minimum requirements of NIST.
Please add 1 or more of the following criteria:
At least one lower case letter.


It is actually missing one more criteria, but for some reason it's not printing it out like how it is for just the lower case one.
Mar 1, 2016 at 11:32am
The reason is else if (lines 55/58). Remove else.
else on line 61 needs to be if (hasSymbol != true)
Mar 1, 2016 at 7:31pm
Oh, I see. My mistake. Thank you very much!
It works perfectly fine now.
Last edited on Mar 1, 2016 at 7:31pm
Mar 1, 2016 at 9:08pm
Don't compare bool variables to true or false. Just use
if ( hasUpperCase ) // true or
if ( !hasUpperCase ) // false
Topic archived. No new replies allowed.