Assumption is that you run in terminal and the terminal shows the I/O. There are codes, special characters, which the terminals interpret and do things like clearing the screen or change of text colour. ANSI-compatible terminals have one set, but Microsoft terminals have different method.
However, if the process sleeps, it is not reading the user input. Notice how sleep_for is part of <thread>. One thread shows the message, starts second thread, and then waits for user input. The second thread sleeps first and then clears the message.
GUI framework libraries tend to provide such "timer" functionality (for GUI applications).