我需要编写一个允许用户选择密码的程序。密码必须是不包含空格的单个字符串。密码验证程序
密码必须为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";
}
}
我建议你为每个验证创建单独的函数,然后按期望的顺序将它们链接在一起。它保持它的灵活性和脱节。 –