See this related question: scanf() is not waiting for user input
The reason is that when you press enter with a char, scanf failed and didn't eat up the char in the input feed. As a result, the next block begins having whatever you entered before.
You can check that by adding a getchar()
before the scanf()
inside the while
loop. You'll notice that it'll repeat the while loop as many times as your line has invalid characters, then stop and wait for input. The getchar()
ate one of the invalid chars in the input each time the loop ran.
It would be better not to use scanf
like that, though. Take a look a this resource:
Reading a line using scanf() not good?