英文:
I am trying to make a password checker to see whether the password is between 8 - 20 characters, & has an uppercase, but cant seem to figure it out
问题
这是你的代码 - 程序运行良好,但是当创建用户时,如果密码仍然只有2个字符,例如,仍然显示为成功注册。 不太确定接下来该怎么做(对编程不太熟悉,这是我第一个独立完成的项目,寻求指导)。(如果代码有点混乱,抱歉)。
void registration() {
system("cls");
bool valid = false;
int minLength = 8;
int maxLength = 20;
string regUsername, regPassword, regUser, regPass;
std::cout << "欢迎来到注册页面!\n";
std::cout << "请输入用户名:\n";
std::cin >> regUsername;
std::cout << "请输入一个8-20字符的密码,其中包含大写和小写字母:\n";
std::cin >> regPassword;
if (regPassword.length() >= 8 && regPassword.length() <= 20) {
for (int k = 0; k < regPassword.length(); k++)
{
if (isupper(regPassword[k]))
{
valid = true;
}
}
}
if (valid == true) {
std::ofstream registerUser("records.txt", std::ios::app); // 打开records.txt以写入用户名和密码。
registerUser << regUsername << " " << regPassword << std::endl;
system("cls");
int choice;
std::cout << "欢迎" << regUsername << ",您已成功注册!\n";
std::cout << "如果要登录,请按1,如果要返回主菜单,请按2。\n";
std::cin >> choice;
switch (choice)
{
case 1: login();
break;
case 2: main();
break;
default:
std::cout << "无效的输入,返回主菜单。\n";
main();
break;
}
}
else if (valid == false) {
std::cout << "无效的密码,请再次尝试创建帐户。\n";
registration();
}
}
我期望密码会被验证,并检查是否为8-20个字符,并且密码中是否有大写字母。我尝试将 &&
更改为 ||
,但我不认为问题出在那里。
英文:
This is the code - the programme runs fine but when creating a user, if the password is still 2 characters long for example, it still shows as successful registration. Not exactly sure where to go from here (new to coding and its my first project solo, looking for guidance.) (sorry if its a clusterbomb of code its abit all over the place)
void registration() {
system("cls");
bool valid = false;
int minLength = 8;
int maxLength = 20;
string regUsername, regPassword, regUser, regPass;
std::cout << "Welcome to the registration page!" << '\n';
std::cout << "Please enter a username: " << '\n';
std::cin >> regUsername;
std::cout << "Enter a password that is 8-20 characters long, with an uppercase and lowercase \n";
std::cin >> regPassword;
if (regPassword.length() >= 8 || regPassword.length() <= 20) {
for (int k = 0; k < regPassword.length(); k++)
{
if (isupper(regPassword[k]))
{
valid = true;
}
}
}
if (valid = true) {
std::ofstream registerUser("records.txt", std::ios::app); // open records.txt to write username and password inside.
registerUser << regUsername << " " << regPassword << std::endl;
system("cls");
int choice;
std::cout << "Welcome aboard " << regUsername << ", your registration has been successful!" << std::endl;
std::cout << "Press 1 if you would like to login, and 2 if you would like to return to the main menu." << std::endl;
std::cin >> choice;
switch (choice)
{
case 1: login();
break;
case 2: main();
break;
default:
std::cout << "Invalid input, returning to main menu." << std::endl;
main();
break;
}
}
else if (valid = false) {
std::cout << "Invalid password, please attempt to create an account again." << '\n';
registration();
}
}
I expected the password to be validated and checked for 8-20 characters along with if there is an uppercase within the password. i have tried changing the && to || but i dont think thats where the problem is.
答案1
得分: 1
我已经为您实现了一个处理C++中密码复杂性的简单类。请看一下。
#include <iostream>
#include <string>
#include <regex>
class PasswordConstraint {
private:
int minLength = 8; // minLength的默认值
int maxLength = 20; // maxLength的默认值
bool requireLowercase = false; // requireLowercase的默认值
bool requireUppercase = false; // requireUppercase的默认值
bool requireNumbers = false; // requireNumbers的默认值
bool requireSpecialChars = false; // requireSpecialChars的默认值
public:
int getMinLength() const {
return minLength;
}
void setMinLength(int length) {
minLength = length;
}
int getMaxLength() const {
return maxLength;
}
void setMaxLength(int length) {
maxLength = length;
}
bool isRequireLowercase() const {
return requireLowercase;
}
void setRequireLowercase(bool value) {
requireLowercase = value;
}
bool isRequireUppercase() const {
return requireUppercase;
}
void setRequireUppercase(bool value) {
requireUppercase = value;
}
bool isRequireNumbers() const {
return requireNumbers;
}
void setRequireNumbers(bool value) {
requireNumbers = value;
}
bool isRequireSpecialChars() const {
return requireSpecialChars;
}
void setRequireSpecialChars(bool value) {
requireSpecialChars = value;
}
bool isLengthValid(const std::string& value) const {
return value.length() >= minLength && value.length() <= maxLength;
}
bool hasLowercase(const std::string& value) const {
return std::regex_search(value, std::regex("[a-z]"));
}
bool hasUppercase(const std::string& value) const {
return std::regex_search(value, std::regex("[A-Z]"));
}
bool hasNumbers(const std::string& value) const {
return std::regex_search(value, std::regex("[0-9]"));
}
bool hasSpecialChars(const std::string& value) const {
return std::regex_search(value, std::regex("[!@#%^&*(),.?\":{}|<>]"));
}
};
void validatePassword(const std::string& value, const PasswordConstraint& constraint) {
if (!constraint.isLengthValid(value)) {
std::cout << "Error: 密码长度应在 " << constraint.getMinLength()
<< " 到 " << constraint.getMaxLength() << " 之间" << std::endl;
}
if (constraint.isRequireLowercase() && !constraint.hasLowercase(value)) {
std::cout << "Error: 密码应包含至少一个小写字母" << std::endl;
}
if (constraint.isRequireUppercase() && !constraint.hasUppercase(value)) {
std::cout << "Error: 密码应包含至少一个大写字母" << std::endl;
}
if (constraint.isRequireNumbers() && !constraint.hasNumbers(value)) {
std::cout << "Error: 密码应包含至少一个数字" << std::endl;
}
if (constraint.isRequireSpecialChars() && !constraint.hasSpecialChars(value)) {
std::cout << "Error: 密码应包含至少一个特殊字符" << std::endl;
}
}
int main() {
std::string password;
std::cout << "请输入密码: ";
std::cin >> password;
PasswordConstraint constraint;
constraint.setMinLength(8);
constraint.setMaxLength(20);
constraint.setRequireLowercase(true);
constraint.setRequireUppercase(true);
constraint.setRequireNumbers(true);
constraint.setRequireSpecialChars(true);
validatePassword(password, constraint);
return 0;
}
您可以自定义validatePassword函数的输出格式。它可以返回布尔值而不是简单地输出到控制台,或者尝试抛出异常,并在稍后的地方捕获异常。
英文:
I've implemented for you a simple class that deals with password complexity in c++. Please take a look.
#include <iostream>
#include <string>
#include <regex>
class PasswordConstraint {
private:
int minLength = 8; // Default value for minLength
int maxLength = 20; // Default value for maxLength
bool requireLowercase = false; // Default value for requireLowercase
bool requireUppercase = false; // Default value for requireUppercase
bool requireNumbers = false; // Default value for requireNumbers
bool requireSpecialChars = false; // Default value for requireSpecialChars
public:
int getMinLength() const {
return minLength;
}
void setMinLength(int length) {
minLength = length;
}
int getMaxLength() const {
return maxLength;
}
void setMaxLength(int length) {
maxLength = length;
}
bool isRequireLowercase() const {
return requireLowercase;
}
void setRequireLowercase(bool value) {
requireLowercase = value;
}
bool isRequireUppercase() const {
return requireUppercase;
}
void setRequireUppercase(bool value) {
requireUppercase = value;
}
bool isRequireNumbers() const {
return requireNumbers;
}
void setRequireNumbers(bool value) {
requireNumbers = value;
}
bool isRequireSpecialChars() const {
return requireSpecialChars;
}
void setRequireSpecialChars(bool value) {
requireSpecialChars = value;
}
bool isLengthValid(const std::string& value) const {
return value.length() >= minLength && value.length() <= maxLength;
}
bool hasLowercase(const std::string& value) const {
return std::regex_search(value, std::regex("[a-z]"));
}
bool hasUppercase(const std::string& value) const {
return std::regex_search(value, std::regex("[A-Z]"));
}
bool hasNumbers(const std::string& value) const {
return std::regex_search(value, std::regex("[0-9]"));
}
bool hasSpecialChars(const std::string& value) const {
return std::regex_search(value, std::regex("[!@#%^&*(),.?\":{}|<>]"));
}
};
void validatePassword(const std::string& value, const PasswordConstraint& constraint) {
if (!constraint.isLengthValid(value)) {
std::cout << "Error: Password should have a minimum length of " << constraint.getMinLength()
<< " and a maximum length of " << constraint.getMaxLength() << std::endl;
}
if (constraint.isRequireLowercase() && !constraint.hasLowercase(value)) {
std::cout << "Error: Password should contain at least one lowercase letter" << std::endl;
}
if (constraint.isRequireUppercase() && !constraint.hasUppercase(value)) {
std::cout << "Error: Password should contain at least one uppercase letter" << std::endl;
}
if (constraint.isRequireNumbers() && !constraint.hasNumbers(value)) {
std::cout << "Error: Password should contain at least one number" << std::endl;
}
if (constraint.isRequireSpecialChars() && !constraint.hasSpecialChars(value)) {
std::cout << "Error: Password should contain at least one special character" << std::endl;
}
}
int main() {
std::string password;
std::cout << "Enter a password: ";
std::cin >> password;
PasswordConstraint constraint;
constraint.setMinLength(8);
constraint.setMaxLength(20);
constraint.setRequireLowercase(true);
constraint.setRequireUppercase(true);
constraint.setRequireNumbers(true);
constraint.setRequireSpecialChars(true);
validatePassword(password, constraint);
return 0;
}
You can customize the format of validatePassword function. It can return boolean instead of a simple output to the console or try to throw exception and catch it somewhere later.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论