The code below simplifies your I / O problems.
" %lf " provides scanf () format 3 directives: 1) white space 2) double qualifier, 3) white space.
The first white space directive is not needed, as %lf allows optional leading spaces. The second white-space directive causes problems because it consumes the Enter key, waiting for additional input.
"%lf" supplies 1 scanf() format directives: double specifier
This consumes additional leading white space, including any previous \ n, then scans and, hopefully, decodes the double. A \n (Enter key), following the number, remains in the input buffer for the next input (next scanf ()).
Now about why your Z control is not working . It looks like your console is consuming ^ Z and doing "pause". My console, instead of passing ^ Z to the program, closes the stdin input. Essentially puts EOF in stdin.
So you need either:
1) Find a console mechanism to close stdin - others suggested ^ D.
2) Use an alternative to exit the loop. I suggest result != 1 , which easily occurs when entering non-numeric input.
#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); }
source share