1

我正在创建一个程序,允许用户键入他们的用户名和密码。问题是,当系统提示“输入用户名:”并且我按回车键时,它会打印出“名称不能包含空白”

但是,如果我按了几个空格键并按回车键并在此之后将其设为空白字段,它会跳过提示用户输入密码而不打印出“名称不能包含空白”并提示用户再次输入用户名。

我应该如何更改我的代码以确保即使我按空格键并按 Enter,它仍会提示用户再次输入用户名?请指教。谢谢

代码

string userName=" ";
string password;
cout << "Enter UserName:";

while (getline(cin, userName)) {
    if (userName.empty()) {

        cout << "Name cannot contain a blank."<< endl;
        cout << "Enter userName:";
        userName = userName;
        //throw errorMsg;
    } 

    if(!userName.empty()) {
        cout << "Enter Password";
    }
}
4

6 回答 6

2

出于多种原因,用户名验证并非易事。您不希望从事制造您“认为”用户想要键入的内容的业务,同时很明显您希望避免对无效的内容进行潜在的长时间验证。

最后,我怀疑您可以简单地获取潜在客户条目,去掉所有空格,如果有任何剩余,请提交原始条目以供验证。不要放弃验证用户可能想要输入的内容。IE

"\t\t   "

应该是重新提示的理由,而

"John Smith"
"\t WillJohnson "
"Gary"

都应该逐字提交,让筹码落在他们可能的地方。

也就是说,

bool isValidUserName(std::string name)
{
    name.erase(std::remove_if(name.begin(), name.end(),
      [](char c){ return std::isspace(static_cast<unsigned char>(c));}), name.end());
    return !name.empty();
}

应该为你做。示例如下:

int main()
{
    std::cout << std::boolalpha << isValidUserName("\t \t ") << std::endl;
    std::cout << std::boolalpha << isValidUserName("\t Will Johnson ") << std::endl;
    return 0;
}

输出

false
true
于 2013-10-06T07:26:09.963 回答
2

假设 C++11 兼容编译器您的空格测试可能使用std::find_if

 if (std::find_if(userName.begin(), userName.end(), isspace))
      != userName.end())

或者

 if (std::find_if(userName.begin(), userName.end(), 
     [=](char c){return isspace(c);}) != userName.end())

请注意,有几个字符类似于空格,' '但也有'\t'(制表)等......

于 2013-10-06T06:48:02.750 回答
1

只需将空格视为一个字符并在找到它时将其递增

于 2013-10-06T06:51:14.407 回答
0
  1. 检查字符串是否为空,如果是,则进行空验证
  2. 如果字符串不为空,则检查它是否有空格,即find_first_not_of("\t ")如果返回大于 0 的数字,我们知道用户名有前导空白/制表符空格,后跟零个或多个字符。即前导空格,然后是用户名
  3. 现在由您来验证一个好的用户名。

    #include <iostream>
    #include <cstring>
    
    using namespace std;
    
    int main() {
        string userName = " ";
        string password;
        cout << "Enter UserName:";
    
    
        while(getline(cin, userName)) {
            if(userName.empty()) {
                cout << "Empty String";
            } else if(userName.find_first_not_of("\t ") > 0) {
                    cout << "Could contain spaces followed by username or just spaces";     
            } else {
                cout << "User Name is Valid";
            }
        }
    
        return 0;
    }
    
于 2013-10-06T10:54:29.557 回答
0

The logic is to check if the starting character of the username is a blank space and also the last slot of the string is not a blank space. example :
This should be accepted = "a b". But
this should not be " a ". or this should not be " a". or this should not be "a ".

if (userName[0]!=" " && userName[userName.size()]!=" "){
       //accept this case and move on.
}
else{
         //reject all other cases.
}
于 2013-10-06T07:51:11.140 回答
0
#include <iostream>
#include <vector>
using namespace std;    

int main(){
    string username, password;
    int un = 1;
    int ps = 1;
    while(un){
    cout<<"Username:";
    getline(cin,username);
    int usize = username.size();
    vector <string> v ;
    v.insert(v.begin(), username);
    if (usize==1){
        if(username==" "){
            cout<<"Username can not be blank"<<endl;
        }
        else{
            un=0;
        }
    }   
    else{
        int check=0; int trim = 0;
        for (int i=0; i<usize; i++){
            if (username[i]!=' '){
                check=1;
                trim = i;
                break;
            }
        }
        if(check==1){
            un=0;
        }
        else{

        }
    }
    }

    while(ps){
    cout<<"Password:";
    getline(cin,password);
    int usize = password.size();
    vector <string> v ;
    v.insert(v.begin(), password);
    if (usize==1){
        if(password==" "){
            cout<<"Password can not be blank"<<endl;
        }
        else{
            ps=0;
        }
    }   
    else{
        int check=0;
        for (int i=0; i<usize; i++){
            if (password[i]!=' '){
                check=1;
                break;
            }
        }
        if(check==1){
            ps=0;
        }
        else{

        }
    }
    }
    cout<<endl;
    cout<<"----------------------------------------------"<<endl;
    cout<<"Username is: "<<username<<endl;
    cout<<"Password is: "<<password<<endl;
    return 0;
}
于 2013-10-06T10:18:43.370 回答