سؤال

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;
}
هل كانت مفيدة؟

المحلول

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).

نصائح أخرى

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)

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

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top