Why code is not printing anything as output?

試著忘記壹切 提交于 2019-12-24 12:20:10

问题


After pressing Ctrl+D, i am expecting this code to print array, but its doing nothing.

#include<stdio.h>

int main(){
    int k,i=0,a;
    int b[10];
    while(scanf("%d",&a)!=EOF){
        if(a>(a/4+a/3+a/2))
        b[i]=a;
        else
        b[i]=(a/4+a/3+a/2);
        i++;
    }
    for(k=0;k<=i;k++){
        printf("%d\n",b[k]);
    }
    return 0;
}

回答1:


You're using the wrong key combination to generate an EOF on your operating system (Windows 8). Ctrl+D is common on unix-like systems, but Windows systems generally use Ctrl+Z.

Note that you might have to use Ctrl+Z twice if you're not on an empty line (once to flush the current line of input, and once to generate the EOF).




回答2:


Best guess -- you're entering something that is not a number, so scanf returns 0 and your program enters an infinite loop. As you don't show your input, it's impossible to tell.

If you want it to stop on a non-number input, change the loop to while(scanf("%d",&a) > 0)




回答3:


you should check the documentation for scanf's return value... it returns the number of items scanned, 0, 1, 2, etc... not EOF



来源:https://stackoverflow.com/questions/22716669/why-code-is-not-printing-anything-as-output

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!