In writing my code for this function, the password always returns as failed-- so it's looping infinitely. If I attempt to follow the pwFailure variable in my head, the passwords I'm inputting should work.
void passTest(char desiredPw[], bool pwFailure)
{
int failCount = 0;
do
{
if (failCount == 0 || failCount == 5)
{
cout << "\nWhen making a password, be sure that it follows these rules:" << '\n' <<
"1. Starts with an alphabetic character." << '\n' <<
"2. There are no spaces or tabs." << '\n' <<
"3. Contains at least one upper case letter." << '\n' <<
"4. Contains at least one special character." << '\n' <<
"5. Contains at least one digit." << '\n' << '\n';
failCount = 0;
}
int hasUpper = 0;
int hasDigit = 0;
int hasSpecChar = 0;
cout << "Now enter your desired password: ";
cin.get(desiredPw, SIZE, '\n');
cin.ignore(100, '\n');
int pwLength = strlen(desiredPw);
//Ensures the password begins with a letter.
if (!isalpha(desiredPw[0]))
{
pwFailure = true;
}
for (int i=0; i<pwLength; ++i)
{
//Checks the password for a space or tab character.
if (isspace(desiredPw[i]))
{
pwFailure = true;
}
//Checks the password for an uppercase.
if (isupper(desiredPw[i]))
{
++hasUpper;
}
//Checks the password for a digit.
if (isdigit(desiredPw[i]))
{
++hasDigit;
}
//Checks the password for a special character.
if (!isalnum(desiredPw[i]))
{
++hasSpecChar;
}
//If the password has none of these, it will fail here.
if (hasUpper == 0 || hasDigit == 0 || hasSpecChar == 0)
{
pwFailure = true;
}
}
//Counts up to five failures and will repeat the rules at 5.
if (pwFailure == true)
{
++failCount;
}
} while (pwFailure == true);
}
constint SIZE = 30;
void passTest (char desiredPw[], bool pwFailure);
int main()
{
char desiredPw[SIZE];
bool pwFailure = false;
cout << "\nWelcome! Let's generate a password-- you can exit the program once you have successfully created a strong password.";
//Calling the passTest function to determine whether or not the password is usable within our parameters.
passTest(desiredPw, pwFailure);
cout << "\nYou have successfully set your password.\n";
return 0;
}
Does the SIZE mess with my isspace?
#include <iostream>
usingnamespace std;
int main()
{
char desiredPw[] = "aBc12B ?";
int pwLength = sizeof(desiredPw) / sizeof(char);
bool pwFailure = false;
if (!isalpha(desiredPw[0]))
{
pwFailure = true;
cout << "Password fails - first character not alpha uppercase" << endl;
}
for (int i = 0; i < pwLength; ++i)
{
//Checks the password for a space or tab character.
if (isspace(desiredPw[i]))
{
pwFailure = true;
cout << "Password fails - space or tab" << endl;
}
/*
//Checks the password for an uppercase.
if (!isupper(desiredPw[i]))
{
++hasUpper;
}
//Checks the password for a digit.
if (isdigit(desiredPw[i]))
{
++hasDigit;
}
//Checks the password for a special character.
if (!isalnum(desiredPw[i]))
{
++hasSpecChar;
}
//If the password has none of these, it will fail here.
if (hasUpper == 0 || hasDigit == 0 || hasSpecChar == 0)
{
//pwFailure = true;
}
*/
}
system("pause");
}
I'd suggest you test it progressively using a small test program with diagnostic messages like this one. ( or maybe somebody will jump in ?)
The other thing is you might need to separately loop through the string several times because the tests are mutually exclusive in a couple of cases.