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



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

问题

尝试:(根据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);
^

获得所需结果的正确方法是什么?我在尝试中需要纠正的不足之处在哪里/什么地方?

我找不到资源的一个弱点是用户输入,因为它似乎没有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;
}

正如@TedLingmo所说:

可能无关:请阅读为什么我不应该#include<bits/stdc++.h>??以及为什么是";使用命名空间std"被认为是不好的做法?被认为是不好的做法?

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

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

最新更新