无限 while 循环 C++ 字符

Infinity while loop C++ char

从这里的 C++ 代码,我尝试创建一个 char 数组来测试输入是否有效,因为它应该以 3 个字母开头,其余 4 个数字(例如 ABC1234)。

函数测试数

bool testNum(char custNum[], int size)
{
    // test first 3 characters for alphabetic letters
    for (int i = 0; i < size - 5; i++)
    {
        if (!isalpha(custNum[i]))
            return false;
    }
    
    // test remaining 4 characters for numeric digits
    for (int i = 3; i < size - 1; i++)
    {
        if (!isdigit(custNum[i]))
            return false;
    }
    
    return true;
}

主程序

int main() 
{
    const int SIZE = 8;
    char customerNum[SIZE];
    
    // loop if customer number not valid
    while (!(testNum(customerNum, SIZE)))
    {

        cout<<"Enter customer number [LLLNNNN]\n(L = Letters | N = Number): ";
        cin.getline(customerNum, SIZE);
        
        // determine a valid customerNum
        if (testNum(customerNum, SIZE))
        {
            cout<<endl;
            for (int i = 0; i < SIZE; i++) 
            {
                cout<<customerNum[i];
            }
            cout<<"is a valid customer number!\n";
        }
        
        else 
        {
            cout<<endl;
            for (int i = 0; i < SIZE; i++)
            {
                cout<<customerNum[i];
            }
            cout<<"is not a valid customer number!\n";
            cout<<"(example of customer number = ABC1234)\n\n";
        }
    } // if user input >= size, somehow the program will loop (ex: 12345678)
    
    return 0;
}

问题出在 while 循环中,例如,如果我尝试输入大于定义大小(例如 12345678)的输入,它将打印输出:

Enter customer number [LLLNNNN]
(L = Letters | N = Number): 12345678

1234567 is not a valid customer number!
(example of customer number = ABC1234)

Enter customer number [LLLNNNN]
(L = Letters | N = Number):
 234567 is not a valid customer number!
(example of customer number = ABC1234)

Enter customer number [LLLNNNN]
(L = Letters | N = Number):
 234567 is not a valid customer number!
(example of customer number = ABC1234)

Enter customer number [LLLNNNN]
(L = Letters | N = Number):
 234567 is not a valid customer number!

在打印输入的输出后,它继续打印一个无限循环,因为程序不会停止让用户输入新值。谁能告诉我为什么会这样?

When the input line is too long, getline sets the failbit of cin, causing each future read from cin to yield an empty string.

我同意对与文本相关的任何内容使用 std::string。 在文本中寻找模式时,没有什么比正则表达式更好的了,它们有很好的文档记录,而且没有经过不重要的测试:)

并且对于数组使用 std::array 而不是 a[] 表示法。它防止了长 运行.

中的很多问题

在这种情况下,测试有效的客户编号使用如下内容:

#include <cassert>
#include <string>
#include <regex>

bool is_valid_customer_number(const std::string& text)
{
    // the next regex matches exactly 3 letters {3}, followed by exactly 4 {4} numbers
    static std::regex expression{ "([a-zA-Z]{3})([0-9]{4})" };
    std::cmatch matches;

    // the next line returns true if the regex matches, 
    // the matches variable is unused but needed to make the call
    return std::regex_match(text.c_str(), matches, expression);
};

main()
{
    assert(is_valid_customer_number("ABC1234"));
    assert(!is_valid_customer_number("1BCA234"));
}

希望这对您的发现之旅有所帮助:)