【问题标题】:looping through a character array c++ [closed]循环遍历字符数组c ++ [关闭]
【发布时间】:2016-07-30 06:54:23
【问题描述】:
#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;

}

所以我想循环一个由 3 个字母和 4 个数字组成的字符数组,比如 ABC1234,但是我没有得到第二个 for 循环的条件(大小 - 1)。每次测试条件时它是如何工作的?

【问题讨论】:

  • 我在任何地方都看不到大小变量声明
  • 你应该使用“break;”而不是“返回 false”
  • 我想他是在问为什么他的第二个循环没有执行?
  • 数组中实际有多少个元素,size 是如何计算的?请注意,像 "ABC1234" 这样的字符串文字使用 8 字符表示 - 字母和数字,加上结尾的 char 值为零 ('\0')
  • 我无法理解你真正的要求。

标签: c++ arrays


【解决方案1】:
  1. 切勿将count 用作循环变量。循环变量的好名字是i

  2. 永远不要在变量初始化的时候声明变量。以上两种情况下都应该是for( int i = 0; ...

  3. i &lt; size - 1 可能是错误的。你可能想要的是i &lt; size

无论如何,如果你展示了size 是如何声明的,它是如何被初始化的等等,它会有所帮助。如果你展示你试图解析的确切文本,它也会有所帮助。如果您确切地解释了您期望发生的事情以及确切发生的事情,这也会有所帮助。当你这样做时,我可能会修改我的答案。

【讨论】:

  • 代码有效。但我为什么要使用 size - 1?
  • Size-1 是有道理的,因为他们不想在最后的 \0 上测试一个数字
  • @Drakes Looping while i &lt; size - 1 如果“大小”是 OP 的实际意思,那将是有意义的。但是从 OP 对命名的掌握程度来看,我们可以很容易地期望“size”不是真正的“size”而是“length”。 (如调用strlen() 的结果。)在这种情况下,循环需要是while i &lt; size。这一切都发生在 OP 的修正案之前,该修正案显示了如何声明和初始化 size
  • 是的,他在 cmets 中添加了更多细节,现在我的上述评论已经过时了。
【解决方案2】:

您只读取了大小变量指定的字符数量, 从那时起,为什么 custNum 函数不会返回真值超过 size 变量? , 因为它只检查大小变量指定的内容。

下面是你需要的代码

      #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;

}

【讨论】:

  • 我的代码运行良好。请向我解释“尺寸 - 1”背后的概念这就是我所要求的。如果我不够清楚,我很抱歉,因为英语是我的第二语言
  • 我的数组大小是8,但是当我输入这个ABC123456789时,它仍然返回true?你能解释一下为什么吗?
  • 当你用 [8] 初始化一个数组时,它实际上可以容纳 9 个元素,因为 0 处有一个元素
  • 0 , 1 , 2 , 3 , 4, 5 , 6, 7,8 , 9 但是如果您修改代码以使用 [7] ,它将解决您的问题,那么它只能8 个元素,包括 0 处的元素
  • 不是ABC123456789,12个元素吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-23
  • 1970-01-01
  • 2011-01-25
  • 1970-01-01
  • 1970-01-01
  • 2014-06-14
  • 2016-01-31
相关资源
最近更新 更多