I think I was wrong about the bad behavior again; it should work. But it is very complicated. The test functions can return false when the password has been changed to a good one. The structure of the while loop corrects for it, but the behavior of the functions is not what one would expect.
The snippet I posted would give the same behavior, but is much simpler.
This would allow the error reporting to be in the loop, and only calls each test once:
Code:
bool passLength(string) // just return true/false. Don't read a new pw or print error
bool containDigit(string) // just return true/false. Don't read a new pw or print error
string getPW()
{
string password;
bool ok=false;
while ( ! ok )
{
cout << "Please enter a password: ";
cin.getline(password, SIZE);
if (! passlength(password)
{
cout << "Passwords must be at least 6 characters long" << endl;
}
else if ( ! containDigit(password) )
{
cout << "Passwords must include at least on digit (1-9)" << endl;
}
else
{
ok=true;
}
}
return password;
}