简体   繁体   中英

Exiting a while loop at EOF using scanf in C

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.

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 Suspended

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!

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

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

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

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()).


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.

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);
}

Okay guys, after giving up on this first part of the project and debugging the other parts, I have determined that my only issue with the code above is I am trying to reference an index of an array that is out of bounds. I have also determined that the EOF key is ctrl-d, even though I have read and been told by my professor that it is ctrl-z for windows. Regardless, this should be an easy fix. Thanks so much to everyone who provided input!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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