2015-04-19 61 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