使用C中的scanf函数在EOF退出while循环 [英] Exiting a while loop at EOF using scanf in C

查看:701
本文介绍了使用C中的scanf函数在EOF退出while循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写我的入门C套餐几个非常小的项目。其中一人要求我在双值阅读,每行一个号码,然后EOF后打印出的基本统计信息。这里是我的我的code,它是给我的问题的片段:

I'm writing a few very small programs for my introductory C course. One of them requires me to read in double values, one number per line, and then print out basic statistics after EOF. Here is my the segment of my code that is giving me issues:

double sample[1000000];
int result;
double number; 
int i = 0;
int count = 0;
double sum = 0;
double harmosum = 0;
result = scanf(" %lf \n", &number); 
double min = number;
double max = number; 

while(result != EOF){

    sample[i] = number;
    if(number < min){
        min = number;
    }
    if(number > max){
        max = number;
    }
    sum += number;
    if(number != 0){
        harmosum += (1 / number);
        count++;
    }
    i++;
    result = scanf(" %lf \n", &number);  
}

这我计算和打印的基础上的数字的一些统计数据。之后

After this I calculate and print some statistics based on the numbers.

我的问题是,我从来没有做出来,它可以扫描每行的循环。为什么是这样?当我preSS Windows上的EOF键(按下Ctrl-Z)控制台说:

My issue is, I am never making it out of the loop that scans each line. Why is this? When I press the EOF key on windows (CTRL-Z?) the console says:

^ Z
暂停

^Z Suspended

,就是这样。闲来无事在程序运行。我曾尝试采取输入从一个文本文件,但也不存在检测文件的末尾。我应该如何解决这个循环?请注意,我只能够使用基本scanf()的无功能的变化。谢谢!

and that is it. Nothing else in the program runs. I have tried taking input from a text file but the end of the file is not detected there either. How should I fix this loop? Note I am only able to use basic scanf() no variation of the function. Thanks!

推荐答案

下面code是你的I / O问题简单化。

The below code is a simplification of your I/O issues.

LF%耗材3 scanf()的格式指令:1)空白2)双符,3)空格

" %lf " supplies 3 scanf() format directives: 1) white-space 2) double specifier, 3) white-space.

不需要第一空白指令为%LF 允许可选的前导空格。第二空白指令导致的问题,因为它消耗的回车键的,等待更多的输入。

The first white-space directive is not needed as the %lf allows for optional leading whitespace. The 2nd white-space directive causes problems as it consumes the Enter key, waiting for additional input.

"%lf" supplies 1 scanf() format directives: double specifier

这消耗了可选的前置空白,包括任何previous \\ n,那么扫描,并希望德codeS双。 A \\ n 回车键的)后的数字留在了下一个输入的输入缓冲区(上接scanf()的)。

This consumes optional leading white-space, including any previous \n, then scans for and hopefully decodes a double. A \n (Enter key) following the number is left in the input buffer for the next input (the next scanf()).

现在以为什么你的控制Z为失败。看来您的控制台消耗^ Z和执行暂停工作。我的控制台上,而不是给^ Z到程序,关闭标准输入的输入。从本质上说把一个EOF的标准输入。

Now as to why your control Z is failing. It appears your console is consuming the ^Z and performing a "suspend job". My console, instead of giving the ^Z to the program, closes the stdin input. In essence putting an EOF in stdin.

所以,你需要或者:结果
1)找到控制台的机制关闭标准输入 - 其他人所说^ D结果。
2)使用替代退出你的循环。我建议的结果!= 1 其中输入非数字输入时容易产生。

So you need to either:
1) Find your console's mechanism for closing the stdin - others have suggested ^D.
2) Use an alternative to exit your loop. I suggest result != 1 which readily occurs when non-numeric input is entered.

#include <stdio.h>
int main(int argc, char *argv[]) {
  int result;
  double x = 0.0;
  do {
    // result = scanf(" %lf ", &x);  /* Your original format */
    result = scanf("%lf", &x);
    printf("%d %lf\n", result, x);
    fflush(stdout);  // Your system may not need this flush
  } while (result != EOF);
}

这篇关于使用C中的scanf函数在EOF退出while循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆