L4> a way to make a cout out disappear

Is there a way to make a cout disappear after a few seconds before the user inputs anything?

example:

Fill in the correct answer

what do planes do?
(disappears after 5 seconds)
user inputs answer.

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.

To disappear after a while means that a process somehow actively keeps track of time.
http://www.cplusplus.com/reference/thread/this_thread/sleep_for/

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).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <chrono>
#include <string>
#include <thread>

using std::chrono::milliseconds;

void delayed_out(const std::string& message, 
    milliseconds letter_delay = milliseconds(200), 
    milliseconds disappearance_delay = milliseconds(2000))
{
    const std::size_t msgLen = message.length();
    const std::string erasure_string = std::string(msgLen, '\b') + 
        std::string(msgLen, ' ') + std::string(msgLen, '\b');

    std::size_t msgIdx = 0;

    while (msgIdx != msgLen)
    {
        std::cout << message[msgIdx++];
        std::this_thread::sleep_for(letter_delay);
    }

    std::this_thread::sleep_for(disappearance_delay);
    std::cout << erasure_string;
}

int main()
{
    delayed_out("What do planes do?");
    std::string response;
    std::cin >> response;
}
cire, it keeps saying that std::chrono hasn't been declared. sorry guys, i'm a fresh beginner.
<chrono> and <thread> require C++11. Make sure you have it enabled for your compiler.
Topic archived. No new replies allowed.