Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 为什么我的for循环只迭代一次?_C_For Loop_Iteration - Fatal编程技术网

C 为什么我的for循环只迭代一次?

C 为什么我的for循环只迭代一次?,c,for-loop,iteration,C,For Loop,Iteration,我正在写一个刽子手程序。我的函数处理用户猜测的字符时遇到问题。我遇到的问题是,我的for循环似乎只迭代了一次,我无法找出原因。这是一个作业,所以我不要求回答具体的代码,但在正确的方向上的一点将是好的。谢谢,功能如下: void charGuess(char *ch,char *word, int aCount) { char *chr; int theResult, i, k, lossCount = 0; char aArray[aCount+1]; char

我正在写一个刽子手程序。我的函数处理用户猜测的字符时遇到问题。我遇到的问题是,我的for循环似乎只迭代了一次,我无法找出原因。这是一个作业,所以我不要求回答具体的代码,但在正确的方向上的一点将是好的。谢谢,功能如下:

void charGuess(char *ch,char *word, int aCount)
{
    char *chr;
    int theResult, i, k, lossCount = 0;
    char aArray[aCount+1];
    char *current;

    for(k=0;k<aCount;k++)
    {
        aArray[k] = '_';
    }

    printf("\nThe char count: %d\n", aCount); //the "aCount" which ive passed in works

    while(1){
        for(i=0; i<aCount; i++) //this loop only iterates once. "aCount" is never 0 when i pass it.
        {
            current = word[i];
            printf("the current char is %c", current);

            if(strcmp(current, ch))
            {
                printf("\ni is: %d\n", i);
                aArray[i] = current;
                printf("%c", aArray[i]);
                displayNewDashes(aCount, aArray);
                break;
            }
            else
            {
                lossCount++;
                hangTheMan(lossCount);
                printf("Loss count: %d", lossCount);
            }
        }
    }
}
void charGuess(char*ch,char*word,int-aCount)
{
char*chr;
结果i,k,lossCount=0;
char aArray[aCount+1];
字符*电流;

对于(k=0;k,因为您声明这是一个赋值,并且您正在寻找指针,所以下面是一些基本的调试步骤

1) 打开编译器能够生成的所有警告,并修复代码,直到警告消失。现在,以上结果(使用gcc):

2) 使用调试器逐步完成代码。如果您的编译器没有调试器,请使用新的编译器。说真的

3) 如果您仍然无法找出循环不正常的原因,请生成仍然具有意外行为的最小示例。很可能,制作一个小示例会暴露问题


4) 如果您仍然看不到它,请在您的问题中记录以上所有内容,我们将很乐意为您提供完成该问题所需的提示。

以及您传递给函数的
aCount
值是多少?当您
中断
时,您将离开循环…@JoachimPileborg“aCount”每次运行程序时都会发生变化,因为这是为hangman生成的随机单词的字符数。此代码中错误的数量使循环提前离开的事实相形见绌。您需要查看C中指针的工作方式。例如:
printf(“当前字符为%C”,current);
不正确。
current=word[I]
将警告提高到淫秽级别并加以解决将大有帮助。@Floris我也尝试过不使用break语句,但它不起作用:(一些提示。在寻找字符匹配时,不要使用strcmp
。将猜测作为
字符传递(而不是
char*
),并在字符串的所有位置上循环:
for(ii=0;ii…或使用
strchr
,其作用相同,但键入较少,不需要另一个局部变量。
hang.c:20:21: warning: incompatible integer to pointer conversion assigning to
      'char *' from 'char'; take the address with & [-Wint-conversion]
            current = word[i];
                    ^ ~~~~~~~
                      &
hang.c:21:46: warning: format specifies type 'int' but the argument has type
      'char *' [-Wformat]
            printf("the current char is %c", current);
                                        ~~   ^~~~~~~
                                        %s
hang.c:26:27: warning: incompatible pointer to integer conversion assigning to
      'char' from 'char *'; dereference with * [-Wint-conversion]
                aArray[i] = current;
                          ^ ~~~~~~~