C++ 我如何将isValidPassword函数添加到此中?
我试图包含一个名为C++ 我如何将isValidPassword函数添加到此中?,c++,C++,我试图包含一个名为isValidPassword的函数,该函数将完成所有检查并返回一个布尔值,该值将指示密码是否有效 #include <iostream> #include <cctype> #include <cstring> using namespace std; // bool isValidPassword(string list); int main() { string password; const int L
isValidPassword
的函数,该函数将完成所有检查并返回一个布尔值,该值将指示密码是否有效
#include <iostream>
#include <cctype>
#include <cstring>
using namespace std;
//
bool isValidPassword(string list);
int main()
{
string password;
const int LENGTH = 101;
char List[LENGTH];
int Upper, Lower, Digit, Special;
cout << "Create your password!\n"
<< "_____________________________________________\n"
<< "Passwords must meet the following criteria:\n" <<
"_____________________________________________\n"
<< "- The password must have at least Tweleve characters. ||\n"
<< "- The password must have at least one Uppercase letter. ||\n"
<< "- The password must have at least one Lowercase letter. ||\n"
<< "- The password must have at least one Digit. ||\n"
<< "- The password must have at least one Special character. ||\n";
do
{
Upper = Lower = Digit = Special = 0;
cout << endl << " Enter password: ";
cin.getline(List, LENGTH);
for (int i = 0; i < strlen(List); i++)
{
if (isupper(List[i]))
Upper++;
if (islower(List[i]))
Lower++;
if (isdigit(List[i]))
Digit++;
if (ispunct(List[i]))
Special++;
}
if (strlen(List) < 12)
cout << "Password needs to have at least twelve characters. ||\n";
if (Upper == 0)
cout << "Password needs to have at least one uppercase letter. ||\n";
if (Lower == 0)
cout << "Password needs to have at least one lowercase letter. ||\n";
if (Digit == 0)
cout << "Password needs to have at least one digit. ||\n";
if (Special == 0)
cout << "Password needs to have at least one special character. ||\n";
} while (Upper == 0 || Lower == 0 || Digit == 0 || Special == 0 || strlen(List) < 12);
}
bool isValidPassword(string password) {}
#包括
#包括
#包括
使用名称空间std;
//
bool isValidPassword(字符串列表);
int main()
{
字符串密码;
常数int长度=101;
字符列表[长度];
整数上限、下限、数字、特殊;
您只需要执行两个步骤:
1.从char[]转到std::string
2.在函数中而不是在主函数中进行验证
#include <iostream>
#include <cctype>
#include <cstring>
using namespace std;
bool isValidPassword(string list);
int main()
{
string password = "";
cout << "Create your password!\n"
<< "_____________________________________________\n"
<< "Passwords must meet the following criteria:\n" <<
"_____________________________________________\n"
<< "- The password must have at least Tweleve characters. ||\n"
<< "- The password must have at least one Uppercase letter. ||\n"
<< "- The password must have at least one Lowercase letter. ||\n"
<< "- The password must have at least one Digit. ||\n"
<< "- The password must have at least one Special character. ||\n";
do
{
cout << endl << " Enter password: ";
cin >> password;
} while (!isValidPassword(password));
cout << "Password is valid!" << endl;
}
bool isValidPassword(string password) {
if (password.length() < 12){
cout << "Password needs to have at least twelve characters. ||\n";
return false;
}
int upper = 0;
int lower = 0;
int digit = 0;
int special = 0;
for (int i = 0; i < password.length(); i++)
{
if (isupper(password[i]))
upper++;
if (islower(password[i]))
lower++;
if (isdigit(password[i]))
digit++;
if (ispunct(password[i]))
special++;
}
if (upper == 0){
cout << "Password needs to have at least one uppercase letter. ||\n";
return false;
}
if (lower == 0){
cout << "Password needs to have at least one lowercase letter. ||\n";
return false;
}
if (digit == 0){
cout << "Password needs to have at least one digit. ||\n";
return false;
}
if (special == 0){
cout << "Password needs to have at least one special character. ||\n";
return false;
}
return true;
}
#包括
#包括
#包括
使用名称空间std;
bool isValidPassword(字符串列表);
int main()
{
字符串密码=”;
看起来您在main
中有一些全面的测试。您是否考虑过将它们移动到isValidPassword
?您的问题是什么?您的代码的哪一部分不能按预期工作?不,它们不能。变量从您开始使用的任何法律字符开始。这是违反约定的,但不是违反规则的h导致编译错误。