从声明中断

时间:2016-12-22 18:22:14

标签: arrays while-loop break enter

当我想扫描一些像1 2 3 4 5这样的数字,然后点击Enter并继续我的代码时,我怎样才能打破我的while语句...这就是我所做的但没有任何作用

while(1){
    res=scanf("%d",&x);
        arr[i++]=x;
        counter++;
            if ( res == 0 ){
                printf("EOF\n");
                break;
            }
            if ( res != 1)
            {
                printf("Nespravny vstup.\n");
                return 1;
            }
            if ( counter > 100)
            {
                printf("Nespravny vstup.\n");
                return 1;
            }
    }
printf("Counter:%d\n", counter);

2 个答案:

答案 0 :(得分:1)

返回打破了整个功能,试试     打破; 而不是回归。

答案 1 :(得分:1)

根据scanf的man页面:

NAME
   scanf,  fscanf, sscanf, vscanf, vsscanf, vfscanf 
...

RETURN VALUE
   These functions return the number of input items  successfully  matched
   and assigned, which can be fewer than provided for, or even zero in the
   event of an early matching failure.

   The value EOF is returned if the end of input is reached before  either
   the  first  successful conversion or a matching failure occurs.  EOF is
   also returned if a read error occurs, in which case the error indicator
   for  the  stream  (see ferror(3)) is set, and errno is set indicate the
   error.

在您的情况下,scanf仅在早期匹配失败的情况下才会返回0