break from while语句

break from while statement

当我想扫描 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);

return中断整个功能,试试 休息; 而不是 return.

根据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 将 return 仅在 早期匹配失败的情况下