Make Output and Input disappear

Hey,
Usually when I write a program with no GUI, the output and user input remains on the screen of the CMD for the duration of the program. How can I control the amount of time that the output remains on the screen?
Can I make my output disappear after a few seconds?
Can I make the user input disappear from the screen after he clicks enter?
Can I put a delay on an output?
Thanks!

Hello yem,

Start with what operating system are you using?
What IDE/compiler are you using?

To put a pause in a program 1 option is:
std::this_thread::sleep_for(std::chrono::seconds(3)); // Requires header files "chrono" and "thread".

How you clear the screen and exactly what small pieces yo want to clear may be more complicated.

If you have not read this take a look. http://www.cplusplus.com/forum/beginner/1988/

If nothing else show some output you get and what you would like it to look like.

Andy
there are libraries and extensions to do all kinds of stuff to the console as well.
All these things are easy with those; some of these can be done without.
I havent felt like doing fancy console in a very, very long time... it was a big deal when I first started and the idea of a gui was new.

A few handy ones you can get for windows (cause you said cmd)
- getch (read a char without showing what was typed)
- gotoxy (move cursor position, one example is back to the start of the current line where you can write spaces over what was just typed... and it 'vanishes')
-there is a clear screen
and more
the portable one is from unix, called ncurses, but there is a windows version so you can use it too.
Last edited on
> Can I make my output disappear after a few seconds?
> Can I make the user input disappear from the screen after he clicks enter?
> Can I put a delay on an output?

You can try this. (Note that this will clear the entire screen and position the cursor at 0,0)

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <cstdlib>
#include <string>
#include <thread>
#include <chrono>

std::ostream& clear_screen()
{
    // generally works on both cmd.exe (windows) and bash (linux etc.)
    // this may also work on tcsh and zsh (needs testing)
    std::system( "@cls || clear" );
    return std::cout ;
}

void sleep_for( unsigned int millisecs )
{
    std::this_thread::sleep_for( std::chrono::milliseconds(millisecs) ) ;
}

std::ostream& print_and_clear_screen( const std::string& txt, unsigned int millisecs_delay )
{
    std::cout << txt << std::flush ;
    sleep_for(millisecs_delay) ;
    return clear_screen() ;
}

std::string read_and_clear_screen( const std::string& prompt, unsigned int millisecs_delay )
{
    std::cout << prompt << ": " ;
    std::string str ;
    std::getline( std::cin, str ) ;
    sleep_for(millisecs_delay) ;
    clear_screen() ;
    return str ;
}

int main()
{
    // clear the screen after a second after input
    const std::string name = read_and_clear_screen( "what is your name", 1000 ) ;

    if( !name.empty() )
    {
        sleep_for(3000) ; // wait for three seconds

        // print the greeting and clear the screen after five seconds
        print_and_clear_screen( "hello " + name + "!\n", 5000 ) ;
    }
}
Thank guys!
I'm going to try some of that.
Thank you guys so much!
I just had a ton of much fun playing around with the info you gave me!

Also, I would like to thank the three of you for always taking the time to answer my questions (I noticed that the three of you seem to always manage to solve my questions. thanks guys)

JLBorges, I love the code you that you gave me. It was exactly what I was looking for. Thank you so much for taking the time to write it for me.
Regarding line 22 (std::cout << txt << std::flush ;) , I had trouble understanding what std::flush does. Would you be able to explain what that output does (If Handy Andy or jonnin can explain that output that would also be great, I'm assuming you guys also understand what that means)?
Last edited on
> Regarding line 22 (std::cout << txt << std::flush ;) , I had trouble understanding what
> std::flush does. Would you be able to explain what that output does

This manipulator may be used to produce an incomplete line of output immediately, e.g. when displaying output from a long-running process, logging activity of multiple threads or logging activity of a program that may crash unexpectedly. An explicit flush of std::cout is also necessary before a call to std::system, if the spawned process performs any screen I/O
https://en.cppreference.com/w/cpp/io/manip/flush
Thanks!
Topic archived. No new replies allowed.