C#
知識:EOF爲文件結束標誌,當SCANF函數未能正確輸入並且輸入ctrl+z時返回-1即EOF的值;
其他情況下 scanf返回輸入正確的個數;
測試代碼如下:
#include<stdio.h>
int sum(int a, int b)
{
return (a + b);
}
int main()
{
int a, b;
while (scanf_s("%d %d", &a, &b)!=EOF)
{
printf("%d\n", sum(a, b));
}
return 0;
}
測試結果:
輸入
ctrl+z ctrl+z ctrl+z scanf返回-1跳出循環
1 2 打印3 scanf返回2正常循環
1 CTRL+z scanf返回1,但scanf彷彿出現故障不能再用,屏幕連續打印隨機數
a B scanf返回0 屏幕連續打印隨機數
C++
很簡單
while(cin>>a)
輸入ctrl+z結束