我應該寫在這裏指定的程序:我必須按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'。你需要重新組織你的程序,所以它不會再等兩次。 –