我需要编写一个允许用户选择密码的程序。密码必须是一个不包含空格的单个字符串。
密码长度必须为6个字符,必须有一个小写字母,必须有一个大写字母,并且必须至少有一个数字。
如果密码失败,则显示有关哪个条件未满足的错误消息。具体说明哪一个为1号。然后让它们重新进入。
用户输入有效密码后,请验证其原始密码。如果条目不匹配,则让它们从头开始。如果有效,则匹配州密码。
示例输出:
> Enter your password: Boy1 Password needs to have 6 or more characters. > Enter your password: women1 Password needs to contain at least one uppercase letter. > Enter your password: boy Password needs to have 6 or more characters. Password needs to contain at least one uppercase letter. Password needs to contain at least one digit. > Enter your password: Mtndew1 Now re-enter your password for verification: Mtndew Password does not match. Start over. > Enter your password: Mtndew1 Now re-enter your password for verification: Mtndew1 You have now entered a valid password.
这是我到目前为止所做的,但不知道如何将所有这些放在一起并使输出功能如上所述我运行它。
#include <iostream>
#include <cctype>
#include <cstring>
using namespace std;
//Function Prototype
void testNum(char[],int);
int main()
{
const int SIZE = 7; //Array Size
char password[SIZE]; //To hold password
int length;
length = strlen(password);
//Get the password.
do{
cout << "Enter your password: ";
cin.getline(password,SIZE);
length = strlen(password);
cout<< "Please enter a password with at least 6 characters.\n";
cin.getline(password,SIZE);
length = strlen(password);
} while (length < 6);
//Call function.
testNum(password,SIZE);
return 0;
}
//Function Definition
void testNum(char pswd[],int size)
{
int count;
for (count = 0; count<size-1; count++)
{
if (!isupper(pswd[count]))
cout << "The password does not contain an uppercase letter.\n";
if (!islower(pswd[count]))
cout << "The password does not contain a lowercase letter.\n";
if (!isdigit(pswd[count]))
cout << "The password does not contain a digit.\n";
}
}
答案 0 :(得分:1)
我经常修改你的代码
你使用const int作为数组大小,但它不能用于另一个函数 所以我使用enum PWSIZE
我在main中循环。如果所有案例都通过,那么打破循环
并且密码数组太小。在lencheck条件中说超过6charset但数组大小为6.它会使缓冲区溢出。我提供你改变std :: string
首先你得到第一次密码和检查案例(如上面的其他地方) 接下来是重新输入检查
看起来有点脏,但这只是概念......
#include <iostream>
#include <cctype>
#include <cstring>
using namespace std;
enum PWSIZE //Array Size
{
PASSWORD_SIZE = 20
};
//Function Prototype
int testNum(char []);
int re_enter(char []);
int main()
{
char password[PASSWORD_SIZE]; //To hold password
int length;
length = strlen(password);
while(1)
{
//Get the password.
do{
cout<< "Please enter a password with at least 6 characters.\n";
cout << "Enter your password: ";
cin.getline(password, PASSWORD_SIZE);
length = strlen(password);
}while(length < 6);
//Call function.
if(testNum(password))
continue; //if return 1 pass below
if(re_enter(password))
continue;
break;
}
return 0;
}
int testNum(char pswd[])
{
int count;
bool upper_flag = 0, lower_flag = 0, digit_flag = 0;
for (count = 0; count<strlen(pswd); count++) //don't need to Size use strlen
{
if (isupper(pswd[count]))
upper_flag = 1;
else if (islower(pswd[count]))
lower_flag = 1;
else if (isdigit(pswd[count]))
digit_flag = 1;
}
if(!upper_flag)
{
cout << "The password does not contain an uppercase letter.\n";
}
if(!lower_flag)
{
cout << "The password does not contain a lowercase letter.\n";
}
if(!digit_flag)
{
cout << "The password does not contain a digit.\n";
}
if(upper_flag && lower_flag && digit_flag)
return 0; //if all pass
else
return 1;
}
int re_enter(char passwd[])
{
char compare_password[PASSWORD_SIZE] = {0,};
cout << "Re Enter Your password" <<endl;
cin.getline(compare_password, PASSWORD_SIZE);
if(strcmp(passwd, compare_password))
{
cout << "Password Not Match" << endl;
return 1;
}
return 0;
}