我应该写在这里指定的程序:我必须按CTRL + D两次才能结束输入,为什么?如何更正?
Input The input will consist of a series of pairs of integers a and b,separated by a space, one pair of integers per line. you should read the input until EOF. Output For each pair of input integers a and b you should output the sum of a and b in one line,and with one line of output for each line in input. Sample Input 1 5 7 2 Sample Output 6 9
一个我这样写:
#includemain() { int a, b; int sum[100]; int i,j; char c; for(i=0; i<100; i++) sum[i]=0; i=0; do { scanf("%d %d", &a, &b); sum[i]=a+b; i++; } while((c=getchar())!=EOF); for(j=0; j<i-1; j++) printf("%d\n", sum[j]); }
什么怪对我来说是:我为什么要按CTRL + d(EOF )两次结束输入?有没有更好的方法来编写这段代码?
一个EOF用于'scanf'函数,一个用于'getchar'。你需要重新组织你的程序,所以它不会再等两次。 –