1

I am trying to collect numbers until EOF is reached, then convert the numbers to English.

$ ./dtoa
22 
twenty two

Here is part of the program:

int num;
while(scanf("%d", &num) != EOF)
        to_string(num);

The problem is, when I enter the input, then press CtrlD for EOF the last number won't actually get printed:

$ ./dtoa
22 33 44(EOF) twenty two
 thirty three(EOF)
 forty four

I need to press CtrlD for the last number to show up. How can I fix it so the full input will be inserted in the first EOF?

Acorn
  • 24,970
  • 5
  • 40
  • 69
avivgood2
  • 227
  • 3
  • 19
  • 1
    press ^D some more times . This is a property of the terminal (pressing it during a line means to flush input , not to close the stream) – M.M Mar 30 '20 at 10:45

1 Answers1

2

To actually send "an EOF" (close the stream), CtrlD must be pressed right after a newline.

Otherwise, the terminal is sending buffered characters (same as an Enter but without adding \n).

Acorn
  • 24,970
  • 5
  • 40
  • 69
  • When I press enter after inserting input the program doesn't give me a chance to enter EOF, and submits the number instantly. so after it finishes the with the numbers the program dosent terminate, and waits for me to enter EOF. – avivgood2 Mar 30 '20 at 10:55
  • @avivgood2 That is expected: first, you press `Enter` to send the input, which gets processed by a first `scanf`, then the program again waits for more input, and at that moment you press `Ctrl` `D` to terminate the stream, which makes the *second* `scanf` call return `EOF`. – Acorn Mar 30 '20 at 10:57