このパスワード検証プログラムを作成しようとしていますが、最初は無限ループに陥っています。とてもイライラします。私はこれに約 1 日半取り組んできました... プログラムの目的は、パスが少なくとも 6 文字の長さであり、上位 1 文字、下位 1 文字、および 1 桁も含まれていることを保証することです。
#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 ;
}