通过验证检查强制输入

时间:2017-07-08 02:09:24

标签: c++ validation input

我们创建了一个验证检查,以便输入只能是字母字符。虽然它按预期工作,但我的朋友和我都遇到了一个奇怪的症状,你可以强制通过验证,即使代码清楚(至少对我们来说)不允许它们。

运行以下代码,然后按[enter]或随机垃圾大约十几次并输入不可接受的输入,例如 321 - 将导致程序退出验证。它是某种固有限制还是我们错过了代码?

即使在将代码剥离到裸露验证之后也会出现这种症状,尽管它看起来似乎有不同的限制,具体取决于机器。有些电脑让它在3-4次暴力强迫后潜行,而c9.io则经受住了大约十几次。

我的验证循环确实看起来很混乱,但我之前也经历过较小的for循环。到目前为止我唯一注意到的相似之处是getline()

#include<iostream>
#include<string>
#include<fstream>

using namespace std;

//prototypes
void validateUserName(string &name);//validate name

//main function
int main()
{

    //initialize
    ifstream file;

    string username; //holds username which serve as file name


    //first prompt
    cout<< "Enter your name and press <ENTER>";
    getline(cin, username);

    //call functions
    validateUserName(username);
    //begin shutdown
    cout << "good-bye" << endl;
    cin.get();
}




void validateUserName(string &name){
    int errCount = 0;
    int i=0;
    do
    {
        errCount = 0;
        i=0;
        //go thru each character of the name to count non-alphabetic character
        while (name[i])
            {
            if ( !isalpha(name[i]) )
            errCount++;
            i++;
            }

        if (errCount > 0 || name.empty() )
        {
            //prompt user again alerting them of error
            cout << "Invalid entry, try again" << endl;
            getline(cin, name);
        }

    }
    while (errCount > 0 || name.empty() );//the validation loop doesn't end until user enters acceptable entry
    name+=".txt"; //appended file type
}

1 个答案:

答案 0 :(得分:0)

当你从第二个cin name出来时,不再是空的。所以第二个条件不满足,你退出主while循环。快速解决方法是在最终条件中增加errCount:

if (errCount > 0 || name.empty() )
{
   errCount++;                                                                                                                                                   
   cout << "Invalid entry, try again" << endl;
   getline(cin, name);
}

虽然这不是你问题的一部分,但我建议你使用string::size()而不是检查空字符。我不认为getline会添加这个,所以你可能会处理未定义的行为。

void validateUserName(string &name)
{
    int errCount = 0;
    do
    {
        errCount = 0;
        //go thru each character of the name to count non-alphabetic character                                                                                                                              
        for (int i = 0; i < name.size() && !name.empty(); i++)
        {
            if ( !isalpha(name[i]) ) 
            {
                errCount++;
                break;
            }
        }

        if (errCount > 0 || name.empty() )
        {
            errCount++;
            //prompt user again alerting them of error                                                                                                                                                      
            cout << "Invalid entry, try again" << endl;
            getline(cin, name);
        }

     }
    while (errCount > 0 || name.empty() );//the validation loop doesn't end until user enters acceptable entry                                                                                              

}