C ++如何检测空格

时间:2013-10-06 06:42:50

标签: c++ visual-c++ c++11

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

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

我应该如何更改我的代码以确保它仍然会提示用户再次输入userName,即使我点击了一个空格键并按下回车键?请指教。感谢

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";
    }
}

6 个答案:

答案 0 :(得分: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'(制表)等...

答案 1 :(得分:2)

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

最后,我怀疑你可以简单地获取潜在客户条目,删除所有空格,如果还有剩余,请提交原始条目进行验证。不要试图验证用户可能打算输入的内容。即。

"\t\t   "

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

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

应该全部提交 verbatim ,让筹码落到他们可能的地方。

那就是说,

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

答案 2 :(得分:1)

将空格视为一个字符,并在找到它时增加它

答案 3 :(得分:0)

逻辑是检查用户名的起始字符是否为空格,并且字符串的最后一个插槽也不是空格。  例如:
 这应该被接受=“a b”。  但
 这不应该是“一个”。  或者这不应该是“a”。  或者这不应该是“a”。

if (userName[0]!=" " && userName[userName.size()]!=" "){
       //accept this case and move on.
}
else{
         //reject all other cases.
}

答案 4 :(得分: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;
}

答案 5 :(得分: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;
    }