簡體   English   中英

我將如何為此添加一個 isValidPassword 函數?

[英]How would I add a isValidPassword function to this?

我正在嘗試包含一個名為isValidPassword的函數,該函數將完成所有檢查並返回一個布爾值,該值將指示密碼是否有效。

#include <iostream>
#include <cctype>
#include <cstring>
using namespace std;


//  
bool isValidPassword(string list);

int main()
{
     string password;

     const int LENGTH = 101;
     char List[LENGTH];
     int Upper, Lower, Digit, Special;

    cout << "Create your password!\n"
         << "_____________________________________________\n"
         << "Passwords must meet the following criteria:\n" <<
         "_____________________________________________\n"
         << "- The password must have at least Tweleve characters.      ||\n"
         << "- The password must have at least one Uppercase letter.    ||\n"
         << "- The password must have at least one Lowercase letter.    ||\n"
         << "- The password must have at least one Digit.               ||\n"
         << "- The password must have at least one Special character.   ||\n";

    do
    {
        Upper = Lower = Digit = Special = 0;

        cout << endl << " Enter password: ";
        cin.getline(List, LENGTH);

        for (int i = 0; i < strlen(List); i++)
        {
            if (isupper(List[i]))
                Upper++;
            if (islower(List[i]))
                Lower++;
            if (isdigit(List[i]))
                Digit++;
            if (ispunct(List[i]))
                Special++;
        }

        if (strlen(List) < 12)
            cout << "Password needs to have at least twelve characters.        ||\n";
        if (Upper == 0)
            cout << "Password needs to have at least one uppercase letter.     ||\n";
        if (Lower == 0)
            cout << "Password needs to have at least one lowercase letter.     ||\n";
        if (Digit == 0)
            cout << "Password needs to have at least one digit.                ||\n";
        if (Special == 0)
            cout << "Password needs to have at least one special character.    ||\n";
    } while (Upper == 0 || Lower == 0 || Digit == 0 || Special == 0 || strlen(List) < 12);
}

bool isValidPassword(string password) {}



你只需要做兩步:
1. 從 char[] 到 std::string
2. 在函數中驗證而不是在主函數中

#include <iostream>
#include <cctype>
#include <cstring>
using namespace std;
bool isValidPassword(string list);
int main()
{
    string password = "";
    cout << "Create your password!\n"
         << "_____________________________________________\n"
         << "Passwords must meet the following criteria:\n" <<
            "_____________________________________________\n"
         << "- The password must have at least Tweleve characters.      ||\n"
         << "- The password must have at least one Uppercase letter.    ||\n"
         << "- The password must have at least one Lowercase letter.    ||\n"
         << "- The password must have at least one Digit.               ||\n"
         << "- The password must have at least one Special character.   ||\n";
    do
    {
        cout << endl << " Enter password: ";
        cin >> password;

    } while (!isValidPassword(password));
    cout << "Password is valid!" << endl;
}

bool isValidPassword(string password) {
    if (password.length() < 12){
        cout << "Password needs to have at least twelve characters.        ||\n";
        return false;
    }
    int upper = 0;
    int lower = 0;
    int digit = 0;
    int special = 0;
    for (int i = 0; i < password.length(); i++)
    {
        if (isupper(password[i]))
            upper++;
        if (islower(password[i]))
            lower++;
        if (isdigit(password[i]))
            digit++;
        if (ispunct(password[i]))
            special++;
    }
    if (upper == 0){
        cout << "Password needs to have at least one uppercase letter.     ||\n";
        return false;
    }
    if (lower == 0){
        cout << "Password needs to have at least one lowercase letter.     ||\n";
        return false;
    }
    if (digit == 0){
        cout << "Password needs to have at least one digit.                ||\n";
        return false;
    }
    if (special == 0){
        cout << "Password needs to have at least one special character.    ||\n";
        return false;
    }
    return true;
}

您可以在此處在線運行: https : //onlinegdb.com/BkoEWg2ar
順便說一句:變量總是以小寫開頭。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM