View Single Post
Old 11-23-2010, 12:21 PM   #15
Happy Monkey
I think this line's mostly filler.
 
Join Date: Jan 2003
Location: DC
Posts: 13,575
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;
}
__________________
_________________
|...............| We live in the nick of times.
| Len 17, Wid 3 |
|_______________| [pics]
Happy Monkey is offline   Reply With Quote