2015-04-19 60 views
-1

我需要编写一个允许用户选择密码的程序。密码必须是不包含空格的单个字符串。密码验证程序

  1. 密码必须为6个字符长,必须有一个小写字母,必须有一个大写字母,并且必须至少有一个数位。

  2. 当密码失败时,显示关于不符合哪个标准的错误消息。具体指出哪一个编号为1.然后让他们重新进入。

  3. 一旦用户输入有效的密码,让他们验证他们原来选择的密码。如果条目不匹配,让他们从头开始。如果它匹配状态密码,如果有效。

样本输出:

 
> 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"; 
    } 

} 
+3

我建议你为每个验证创建单独的函数,然后按期望的顺序将它们链接在一起。它保持它的灵活性和脱节。 –

回答

1

我修改您的代码有很多

  1. 您使用const int的数组大小,但它不能在其他功能 所以我用枚举PWSIZE使用

  2. 我做循环主要。如果全部情况都通过,则打破循环

  3. 并且密码数组大小太小。在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; 
} 
+0

我认为可能最好检查最大长度检查大声笑 – jen6