【问题标题】:Unknown type name 'count' at cc 处的未知类型名称“计数”
【发布时间】:2021-10-29 09:44:08
【问题描述】:

我想将数组 string[1000][1000] 中的所有字符串与字符串中超过 10 个字母进行比较。所以在我的代码中,我使用嵌套循环尝试将其与数组中的其他字符串进行比较。但它说代码给我这个错误“未知类型名称'count'”,“[Error] 预期的声明说明符或'...'在字符串常量之前”和“[Error] 预期的标识符或'('之前' }' 令牌" 为什么会一直这样?

void main() 
{
char string[1000][1000];
int i, o = 1, sentence;

printf("enter the number of speeches: ");
scanf("%d", &sentence);

for (i = 0; i < sentence; i++)
{
    printf("\nplease enter speech number %d: ", o);
    scanf("%s", &string[i]);
    o++;
}


int q, w, t, count = 0;

for(q = 0; q < sentence; q++)
{
    t = strlen(string[q]);
    if (t >= 10)
    {
        if (q = 0)
        {
        count++;
        }
        
        else 
        {
            for(w = 0; w < q; w++)
            {
                if (strcmp(string[q], string[w]) == 0)
                {
                    count += 0;
                    break;
                }
                else
                    count++;
            }
        }               
        }
    }
}
printf("%d", count);
}

【问题讨论】:

  • 在这两个 } 中具有相同缩进的可能是错误的。
  • 不相关:scanf("%s", string[i]);(没有&amp;)和if (q == 0)(有比较而不是赋值)
  • count += 0; 没有任何用处,你不需要它。
  • 此外,您的数组string 占用了几乎整个 MiB 的内存,这使得它非常接近 Windows 上的堆栈限制(默认情况下,它只为每个进程提供一个 MiB 堆栈)。在读取sentence 的值后使用指针和动态分配,或者可能使用可变长度数组。
  • 一致的代码风格真的很有帮助。这包括缩进,如果您要在if 上使用花括号,请在相应的else 上使用它们。

标签: c debugging multidimensional-array error-handling nested


【解决方案1】:

你有两个错误。

for(q = 0; q < sentence; q++)
{
    t = strlen(string[q]);
    if (t >= 10)
    {
        if (q == 0)
        {
            count++;
        }
        
        else 
        {
            for(w = 0; w < q; w++)
            {
                if (strcmp(string[q], string[w]) == 0)
                {
                    count += 0;
                    break;
                }
                else {
                    count++;
                }
            }               
        }
    }
}

【讨论】:

  • 请在您的回答中提供更多详细信息。正如目前所写的那样,很难理解您的解决方案。
  • 您忘记删除无用的count += 0; 分配。
猜你喜欢
  • 2018-10-27
  • 1970-01-01
  • 2013-09-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-26
相关资源
最近更新 更多