繁体   English   中英

刷新输入缓冲器C.

[英]Flush Input Buffer C

注意:fflush(stdin)不起作用。

问题:
我使用scanf(inb4折旧)在while循环中输入数字。 当我输入一个时,缓冲区用空行填充其余部分。

码:

double input, total;

for(i=0; i<COUNT; i++){
     printf("\nNumber %d: ", i+1);
     scanf("%d", &input);
     total += input;
}
printf("\nAverage: %f\n", total/COUNT);

输出:

Please enter 5 decimal numbers: 
Number 1: 1.0

Number 2: 
Number 3: 
Number 4: 
Number 5: 
Average: 0.000000
 if(scanf("%d", &input) != 1)
 {
      /* If scanf failed to read a number clear the input buffer */
      while((c = getchar()) != '\n' && c != EOF);
 }

在获取输入之前使用flushall()您还使用%d存储为double,使用%lf,或将输入声明为int

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM