compilation - 在我的编译器中完全可以正常工作,但在将其提交到在线社区时会出现编译错误

这段代码在我的电脑上工作得很好,而我用 codeblocks 10.05 编译它。该代码不会给出任何错误或警告!但是,在将其提交给在线法官编码社区时,他们以编译错误回复我!
这是我的代码:

#include<stdio.h>
#include<stdlib.h>
int main()
{
    int j=1;
    while(j=1){
        int x,y,i,j,num,count=0,p,k;
        for(;;){
            printf("enter two integers. they must not be equal and must be between 1 and 100000\n");
            scanf("%d%d",&i,&j);
            if(i>=1 && i<100000 && j>=1 && j<100000 && i!=j){
                break;
            }
            else{
                printf("try the whole process again\n");
            }
        }
        if(i>j){
            x=i;
            y=j;
        }
        else{
            x=j;
            y=i;
        }//making x always greater than y
        int *cyclelength=(int *)malloc(5000*sizeof(int));
        if (NULL==cyclelength){
            printf("process aborted");
            return 0;
        }
        else{
            /*solution part for the range of number. and solution for each number  put into cyclelength.*/
            num=y;
            while(num<=x){
                p=1;
                k=num;
                while(k!=1){
                    if(k%2==0)
                        k=k/2;
                    else
                        k=3*k+1;
                    p+=1;
                    }
                cyclelength[count]=p;
                num+=1;
                count+=1;
            }
            int c=0;
            int max=cyclelength[c];
            for(c=0;c<x-y-1;c+=1){
                if(max<cyclelength[c+1]){
                    max=cyclelength[c+1];
                }
            }
            free(cyclelength);
            cyclelength = NULL;
            printf("%d,%d,%d\n",i,j,max);
        }
    }
}

当我尝试在 下的在线社区中提交它时ANSI C 4.1.2 - 带有选项的 GNU C 编译器:-lm -lcrypt -O2 -pipe -ansi -DONLINE_JUDGE 这种格式。他们给我发了一条编译错误信息!
Our compiler was not able to properly proccess your submitted code. This is the error returned:
code.c: In function 'main':
code.c:25:10: error: expected expression before '/' token
code.c:27:19: error: 'cyclelength' undeclared (first use in this function)
code.c:27:19: note: each undeclared identifier is reported only once for each function it appears in
code.c:10:18: warning: ignoring return value of 'scanf', declared with attribute warn_unused_result

为什么我会得到这个?我的错在哪里??编码格式不匹配??我是初学者!

最佳答案

使用 -ansi 选项,错误是 ANSI 合规性失败:

}//making x always greater than y

应该
} /* making x always greater than y */

在第 26 行;你不能在这里声明周期长度;它需要位于函数的顶部(第 5 行)
int main()
{
    int *cyclelength;
...
    cyclelength=(int *)malloc(5000*sizeof(int));

scanf 问题是一个警告,因为不检查 scanf 的返回值就假定它已经成功;所以你应该做类似的事情:
do {
    printf("enter two integers. they must not be equal and must be between 1 and 100000\n");
    number_read = scanf("%d%d", &i, &j);
    if (number_read < 0) /* read failed, e.g. user entered ctrl-d */
        return 0;
    } while (number_read != 2);

当然你需要声明number_read在函数的顶部

https://stackoverflow.com/questions/11427172/

相关文章:

ruby-on-rails - SCSS编译错误

.net - ObservableCollection-无法实现,因为它没有匹配的返回类型

flash - 无法解析 to a component imple

ruby-on-rails - 使用Mysql在共享服务器中部署Rails应用程序时出错

android - 使用PendingIntent的编译错误

python - 错误 : invalid mode ('r' ) - Cannot conduct

windows - libqxt编译-mingw32-make异常

winapi - InterlockedPushListSList丢失。它在哪里?

android - 修复了C代码中的错字,仍然无法构建

xcode - 项目清理后,Cocos2d/box2d项目将无法编译