Quote:
If I use a file instead of Std, I get correct result..
Your mistake is that the keyboard never ends, the fact that you wait in front of keyboard is not an end of input.
Back in time, when I did this in C, the keyboard end of file was Ctrl-Z.
If it don't work, you will have to think about a special input that will act as an end of file.