C++ 循环通过字符数组c++; #包括 #包括 使用名称空间std; boolcustnum(char[],int); int main() { 常数int size=8; char custmor[大小]; 库特

C++ 循环通过字符数组c++; #包括 #包括 使用名称空间std; boolcustnum(char[],int); int main() { 常数int size=8; char custmor[大小]; 库特,c++,arrays,C++,Arrays,切勿将count用作循环变量。循环变量的好名称是i 永远不要在初始化时声明变量。在这两种情况下,上述变量都应该是for(int i=0;…) i

切勿将
count
用作循环变量。循环变量的好名称是
i

  • 永远不要在初始化时声明变量。在这两种情况下,上述变量都应该是
    for(int i=0;…

  • i
    可能是错误的。您可能想要的是
    i


  • 无论如何,如果你展示了
    size
    是如何声明的,它是如何初始化的,等等,这会很有帮助。如果你展示了你正试图解析的确切文本,它也会很有帮助。如果你准确地解释了你预期会发生什么,以及到底发生了什么,这也会很有帮助。当你这么做的时候,我可能会修改我的答案。

    你只阅读了一篇文章大小变量指定的字符数量, 从那以后,为什么custNum函数对于长度大于size变量的任何内容都不会返回true?因为它不会检查超出size变量指定的任何内容

    下面是您需要的代码

    #include <iostream>
    #include <string>
    
    using namespace std;
    bool custNum(char [], int);
    
    
    int main()
    {
    
        const int size = 8;
        char custmor[size];
    
        cout << "Enter a customer number in the form ";
        cout << "LLLNNNN\n";
        cout << "(LLL = letters and NNNN = numbers): ";
        cin.getline(custmor, size);
        if(custNum(custmor, size))
            cout<<"That's a valid id number"<<endl;
        else
            cout<<"That's not a valid id number"<<endl;
    
    
    
    
            return 0;
    }
    bool custNum(char custNum[], int size)
    {
        int count;
    
        for(count = 0; count<3; count++)
        {
    
            if(!isalpha(custNum[count]))
                return false;
        }
        for(count = 3; count <size - 1; count++) //3<7 , 4
        {
            if(!isdigit(custNum[count]))
                return false;
        }
        return true;
    
    }
    
    #包括
    #包括
    使用名称空间std;
    bool custNum(字符串,无符号整数);
    int main()
    {
    常量无符号整数大小=8;
    //char custmor[大小];
    字符串mystring;
    
    我想他是在问为什么他的第二个循环没有执行?数组中实际有多少个元素,以及如何计算
    size
    大小?请注意,像
    “ABC1234”这样的字符串文字
    使用
    8
    字符表示-字母和数字,以及值为零的尾随
    char
    '\0'
    )我无法理解你的真正要求。代码是有效的。但为什么我要使用大小-1?大小-1是有意义的,因为他们不想在期末考试中测试一个数字\0@Drakes如果“size”,则在
    i
    时循环是有意义的这就是OP的真正含义。但从OP对命名的掌握程度来看,我们同样可以很容易地预期“size”的意思不是真正的“size”,而是“length”(如调用
    strlen()
    )在这种情况下,循环必须是while
    i
    。这是OP的修正案之前的全部内容,该修正案显示了
    size
    是如何声明和初始化的。是的,他在注释中添加了更多细节,这使得我的上述注释现在过时了。我的代码工作得很好。请向我解释“size-1”背后的概念这就是我要问的。很抱歉,如果我不够清楚,因为英语是我的第二语言。数组大小是8,但当我输入这个ABC123456789时,它仍然返回true?你能解释为什么吗?当你用[8]初始化数组时,它实际上可以容纳9个元素,因为00、1、2、3、4、5、6、7、8、9处有一个元素,但如果您将代码修改为使用[7],它将解决您的问题,那么它只能包含8个元素,包括0处的元素不是ABC123456789,12个元素吗?
          #include <iostream>
    #include <string>
    
    using namespace std;
    bool custNum(string,unsigned int);
    
    
    int main()
    {
    
        const unsigned int size = 8;
        //char custmor[size];
    
        string mystring;
    
        cout << "Enter a customer number in the form ";
        cout << "LLLNNNN\n";
        cout << "(LLL = letters and NNNN = numbers): ";
        cin >> mystring;
    
        cout << mystring <<endl << " " <<   mystring.length() << endl;
        // cin.getline(custmor, size);
    
    
         if(custNum(mystring , size))
            cout<<"That's a valid id number"<<endl;
        else
            cout<<"That's not a valid id number"<<endl;
    
    
    
    
            return 0;
    }
    bool custNum(string s, unsigned int size)
    {
        unsigned int count;
    
        if (s.length() != (size + 1))
            return false;
    
        for(count = 0; count<3; count++)
        {
    
            if(!isalpha(s[count]))
                return false;
        }
        for(count = 3; count <size - 1; count++) //3<7 , 4
        {
            cout <<  s[count] <<endl;
            if(!isdigit(s[count]))
                return false;
        }
        return true;
    
    }