如何允许用户输入密码并检查一组要求

How to allow user to enter password and check for set of requirements

我目前正在学习如何用 C++ 编程,其中一个实际示例是编写一个允许用户输入 PW 并检查它是否具有特定类型的字母、数字的程序。目前在粘贴到 .txt 并使用 makefile 编译之前写入 repl.it。我找不到任何资源或类似示例来解释如何执行此操作,我也没有完全掌握足够的知识来识别用于搜索的正确关键字。

Question

尝试:(根据 NathanOliver 的建议编辑)

#include <bits/stdc++.h>
#include <iostream>
#include <cctype>
using namespace std;

//get the password from the user
void printStrongpass(string& input)
{
int n = input.length();
// Check lower alphabet in string
bool hasLower = false, hasUpper = false;
bool hasDigit = false, specialChar = false;
string normalChars = "abcdefghijklmnopqrstu"
    "vwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 ";

for (int i = 0; i < n; i++) {
    if (islower(input[i]))
        hasLower = true;
    if (isupper(input[i]))
        hasUpper = true;
    if (isdigit(input[i]))
        hasDigit = true;
    size_t special = input.find_first_not_of(normalChars);
    if (special != string::npos)
        specialChar = true;
}
// Password pass?
if (hasLower && hasUpper && hasDigit &&
    specialChar && (n >= 8))
    cout << "Valid" << endl;
else if ((hasLower || hasUpper) &&
    cout << "Invalid" << endl;
specialChar && (n >= 6))
    cout << "Invalid" << endl;     
else
    cout << "Invalid" << endl;
specialChar && (n >= 6))
}
// Driver code
int main()
cout << "Please enter your password.\n";
cout << " Your password must be at least 8 characters long.";
cout << "it has no whitespace characters;<< endl;
cout << "whitespace characters are ' ', '\t', '\n', and '\r',<< endl;
cout << "at least 1 character is a digit, 0-9,"<< endl;
cout << "at least 1 character is a lowercase letter, a-z"<< endl;
cout << "at least 1 character is an uppercase letter, A-Z"<< endl;
cout << "at least 1 character is one of these four punctuation       
characters: !@#$" << endl; 
{
cin >> password;
printStrongpass(input);
return 0;
}

除此之外

main.cpp:7:1: error: unknown type name 'cout'
cout << "Please enter your password.\n";
^
main.cpp:7:6: error: expected unqualified-id
cout << "Please enter your password.\n";
 ^
main.cpp:8:1: error: unknown type name 'cout'
cout << " Your password must be at least 8 characters long.";
^
main.cpp:8:6: error: expected unqualified-id
cout << " Your password must be at least 8 characters long.";
 ^
main.cpp:9:1: error: unknown type name 'cout'
cout << "it has no whitespace characters;<< endl;
^
main.cpp:9:6: error: expected unqualified-id
cout << "it has no whitespace characters;<< endl;
 ^
main.cpp:9:9: warning: missing terminating '"' character [-Winvalid-pp-token]
cout << "it has no whitespace characters;<< endl;
    ^

main.cpp:10:9: warning: missing terminating '"' character [-Winvalid- `pp-token]`
cout << "whitespace characters are ' ', '\t', '\n', and '\r',<< endl;
    ^
main.cpp:12:1: error: unknown type name 'cout'
cout << "at least 1 character is a lowercase letter, a-z"<< endl;
^
main.cpp:12:6: error: expected unqualified-id
cout << "at least 1 character is a lowercase letter, a-z"<< endl;
 ^
main.cpp:13:1: error: unknown type name 'cout'
cout << "at least 1 character is an uppercase letter, A-Z"<< endl;
^
main.cpp:13:6: error: expected unqualified-id
cout << "at least 1 character is an uppercase letter, A-Z"<< endl;
 ^
main.cpp:14:1: error: unknown type name 'cout'
cout << "at least 1 character is one of these four punctuation character...
^
main.cpp:14:6: error: expected unqualified-id
cout << "at least 1 character is one of these four punctuation character...
 ^
main.cpp:47:25: error: expected ';' after expression
    specialChar && (n >= 6))
                           ^
                           ;
main.cpp:47:25: error: expected expression
main.cpp:47:14: warning: expression result unused [-Wunused-value]
    specialChar && (n >= 6))
    ~~~~~~~~~~~ ^  ~~~~~~~~
main.cpp:53:12: error: use of undeclared identifier 'password'
cin >> password;
       ^
main.cpp:54:21: error: use of undeclared identifier 'input'
printStrongpass(input);
                ^

获得期望结果的正确方法是什么?where/what我的尝试中有哪些不足需要我改正?

我找不到资源的一个弱点是用户输入,因为它似乎不如 Python 简单:username = input("Enter username:")。

编辑后的错误:

main.cpp:7:1: error: unknown type name 'cout'
cout << "Please enter your password.\n";
^
main.cpp:7:6: error: expected unqualified-id
cout << "Please enter your password.\n";
 clang++-7 -pthread -std=c++17 -o main main.cpp
main.cpp:37:24: error: expected ';' after expression
 clang++-7 -pthread -std=c++17 -o main main.cpp
main.cpp:37:24: error: expected ';' after expression
specialChar && (n >= 6))
                   ^
                   ;
main.cpp:37:24: error: expected expression
main.cpp:37:13: warning: expression result unused [-Wunused-value]
specialChar && (n >= 6))
~~~~~~~~~~~ ^  ~~~~~~~~
main.cpp:41:3: error: expected function body after function declarator
cout << "Please enter your password.\n";
^
main.cpp:42:3: error: unknown type name 'cout'
cout << " Your password must be at least 8 characters long.";
^
main.cpp:42:8: error: expected unqualified-id
cout << " Your password must be at least 8 characters long.";
   ^
main.cpp:43:3: error: unknown type name 'cout'
cout << "it has no whitespace characters;<< endl;
^
main.cpp:43:8: error: expected unqualified-id
cout << "it has no whitespace characters;<< endl;
   ^
main.cpp:43:11: warning: missing terminating '"' character [-Winvalid-pp-token]
cout << "it has no whitespace characters;<< endl;
      ^
main.cpp:44:11: warning: missing terminating '"' character [-Winvalid-pp-token]
cout << "whitespace characters are ' ', '\t', '\n', and '\r',<< endl;
      ^
main.cpp:46:3: error: unknown type name 'cout'
cout << "at least 1 character is a lowercase letter, a-z"<< endl;
^
main.cpp:46:8: error: expected unqualified-id
cout << "at least 1 character is a lowercase letter, a-z"<< endl;
   ^
main.cpp:47:3: error: unknown type name 'cout'
cout << "at least 1 character is an uppercase letter, A-Z"<< endl;
^
main.cpp:47:8: error: expected unqualified-id
cout << "at least 1 character is an uppercase letter, A-Z"<< endl;
   ^
main.cpp:48:3: error: unknown type name 'cout'
cout << "at least 1 character is one of these four punctuation       
^
main.cpp:48:8: error: expected unqualified-id
cout << "at least 1 character is one of these four punctuation       
   ^
main.cpp:48:11: warning: missing terminating '"' character [-Winvalid-pp-token]
cout << "at least 1 character is one of these four punctuation       
      ^
main.cpp:49:19: warning: missing terminating '"' character [-Winvalid-pp-    token]
characters: !@#$" << endl; 
              ^

试试这个:


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

void start_msg() {
    //get the password from the user
    cout << "Please enter your password.\n";
    cout << " Your password must be at least 8 characters long.";
    cout << "it has no whitespace characters;"<< endl;
    cout << "whitespace characters are ' ' (space), tab, newline, and return," << endl;
    cout << "at least 1 character is a digit, 0-9," << endl;
    cout << "at least 1 character is a lowercase letter, a-z" << endl;
    cout << "at least 1 character is an uppercase letter, A-Z" << endl;
    cout << "at least 1 character is one of these four punctuation characters: !@#$" << endl;
}

void printStrongpass(string& input)
{
    int n = input.length();
    // Check lower alphabet in string
    bool hasLower = false, hasUpper = false;
    bool hasDigit = false, specialChar = false;
    string normalChars = "abcdefghijklmnopqrstu"
        "vwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 ";

    for (int i = 0; i < n; i++) {
        if (islower(input[i]))
            hasLower = true;
        if (isupper(input[i]))
            hasUpper = true;
        if (isdigit(input[i]))
            hasDigit = true;
        size_t special = input.find_first_not_of(normalChars);
        if (special != string::npos)
            specialChar = true;
    }
    // Password pass?
    if (hasLower && hasUpper && hasDigit &&
        specialChar && (n >= 8))
        cout << "Valid" << endl;
    else if ((hasLower || hasUpper) &&
        cout << "Invalid" << endl;
        specialChar && (n >= 6))
        cout << "Invalid" << endl;
    else
        cout << "Invalid" << endl;
    //specialChar && (n >= 6))
}
// Driver code
int main()
{
    start_msg();
    std::string password;
    cin >> password;
    printStrongpass(password);
    return 0;
}

正如@TedLyngmo 所说:

Probably unrelated: Please read ? and Why is "using namespace std;" considered bad practice? considered bad practice?

首先,你不能在函数之外进行函数调用等,这就是我添加 start_msg().

的原因

其次,确保正确关闭字符串。