我正在尝试制作这个密码验证程序,并且我在第一时间得到一个无限循环。它非常令人沮丧。我已经为此工作了大约一天半......该程序的目的是确保通行证至少有 6 个字符长,并且有一个上、一个下和一个数字。
#include <iostream>
#include <cstring>
#include <cctype>
using namespace std;
bool testPass(char []);
int main()
{
char *password;
int length;
int num;
cout << "Please enter how many characters you would like your\npassword to be.";
cout << " Your password must be at least 6 characters long." << endl;
cin >> num;
while(num < 6)
{
cout << "Please enter a password length of at least 6 characters." << endl;
cin >> num;
}
password = new char[num+1];
cout << "Please enter a password that contains at least one uppercase letter, ";
cout << "one\nlowercase letter, and at least one digit." << endl;
cin >> password;
length = strlen(password);
while (length != num)
{
cout << "Your password is not the size you requested. ";
cout << "Please re-enter your password." << endl;
cin >> password;
length = strlen(password);
}
if (testPass(password))
cout << "Your password is valid." << endl;
else
{
cout << "Your password is not valid. ";
cout << "Please refer to the above warning message." << endl;
}
delete[] password;
system("pause");
return 0;
}
bool testPass(char pass[])
{
bool aUpper = false,
aLower = false,
aDigit = false ;
for ( int i = 0 ; pass[i] ; ++i )
if ( isupper(pass[i]) )
aUpper = true ;
else if ( islower(pass[i]) )
aLower = true ;
else if ( isdigit(pass[i]) )
aDigit = true ;
if ( aUpper && aLower && aDigit )
return true;
else
return false ;
}