我需要編寫一個允許用戶選擇密碼的程序。密碼必須是不包含空格的單個字符串。密碼驗證程序
密碼必須爲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";
}
}
我建議你爲每個驗證創建單獨的函數,然後按期望的順序將它們鏈接在一起。它保持它的靈活性和脫節。 –