Wednesday 15 January 2014

Exiting a while loop at EOF using scanf in C -


I am writing a few small programs for my introductory course. One of them needs to be read in double values, one row per line, and then print the original figures after the EOF. This is my own segment which is giving me problems:

  double sample [1000000]; Full result; Double number; Int i = 0; Int count = 0; Double zodiac = 0; Double hormosom = 0; Result = scanned ("% lf \ n", and number); Double min = number; Double max = number; While (result! = EOF) {sample [i] = number; If (number & lt; minute) {min = number; } If (number> maximum) {max = number; } Amount + = number; If (number! = 0) {Hanarosam + = (1 / number); Count ++; } I ++; Result = scanned ("% lf \ n", and number); }   

After this I calculate and print some figures based on numbers.

My point is, I'm never taking it out of the loop which scans every line, why is it so? The console says that when I press the EOF key on windows (CTRL-Z) the console says:

^ Z suspended

And this is nothing else in the program I have tried to take input from a text file but at the end of the file or I do not know how should I fix this loop? Note I'm only able to use any variation of basic scanf () function. Thanks!

The code below is the simplification of your I / O problems.

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

The first white space command is not required because % lf allows the optional primary location. The second white-space director causes problems because it enter , is waiting for additional input.

  Supports 1 scanf "% lf" () Format Instructions: Double Specifier   

This optional pioneer uses white-space, in which After any previous \ n, it scans again and is expected to decode a double after the number one \ n ( enter ) input to the next input ( The next scanf ()) is left in the input buffer.


As of now Why your control Z is failing It appears that your console is using ^ z and doing "suspended work" My console, instead of giving the program ^ Z, stdin closes the input briefly to put an EOF in STDIN.

So you need to either: 1) Search for the mechanism of your console to close the studin - others have suggested ^ d.
2) Use an option to exit your loop I suggest that the result! = 1 which easily occurs when non-numeric input is entered.

  #include & lt; Stdio.h & gt; Int main (int argc, char * argv []) {int results; Double X = 0.0; Do {// result = scanf ("% lf", & amp; x); / * Your original format * / result = scanf ("% lf", & amp; x); Printf ("% d% lf \ n", result, x); Fflush (stdout); // Your system does not need this flush} while (result! = EOF); }    

No comments:

Post a Comment