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);
// 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;
}
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;
}
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:
# include <iostream>
# include <string>
usingnamespace 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;
elseif (c >= 97 && c <= 122)
hasLowerCase = true;
elseif (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;
}
elseif (hasLowerCase != true) {
cout << "At least one lower case letter." << endl;
}
elseif (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.