How to delete a line after a few seconds without win API

I am making a program that shows a number then after a few seconds it should erase it having the player remember it the type it in but i dont know how to get it to erase the numbers after lets say, 5 seconds or so. I really dont want to use winapi unless absolutley necessary. how would i do this?

CODE:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <ctime>
#include <random>
#include <string>

using namespace std;

int main()
{
    int X;

    time_t T;
    time (&T);
    srand(T);

    for(int R = 0; R < 5; ++R)
    {
        X = rand() % 1000;
    }

    cout << X << endl;
}
This is not guaranteed to work - though it usually does.

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <ctime>

int main()
{
    std::cout << "waiting for 5 seconds..." ;
    std::time_t start = std::time(0) ;
    while( std::difftime( std::time(0), start ) < 5 ) ;
    std::cout << "\r*************************\n" ;
    std::cin.get() ;
}
As I mentioned to you earlier, you don't need line 16.

But to answer your question:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <cstdlib>
#include <ctime>

int main()
{
    time_t T, U;
    time (&T);
    time (&U);
    srand(T);

    int X = rand() % 1000;

    std::cout << X;

    while (U - T < 5)    
        time(&U);

    std::cout << "\b\b\b\b" << std::endl;;
}
Last edited on
@Stewbond

I believe your last line should be..
std::cout << "\b\b\b\b " << std::endl;// four spaces
otherwise, the cursor would only start at the beginning of the line, but would not be erased.
Last edited on
Topic archived. No new replies allowed.