Thread: Standard way to clear the console (?)

  1. #1
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640

    Standard way to clear the console (?)

    I always see these "how do i clear the console screen" threads and i always see the "use system( ... )" replies. I dont know if this is standard but it works on all platforms ive tried.

    Code:
    cout << "Foo\n" << "bar" << endl;
    ...
    cout << "\033c";  // clear the screen
    fflush(stdout);
    cout << "Bar\n" << "foo" << endl;

  2. #2
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    I tried it on Dev-C++ and it didnt work.. it just printed an arrow on the screen.
    What is C++?

  3. #3
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    Quote Originally Posted by Vicious
    I tried it on Dev-C++ and it didnt work.. it just printed an arrow on the screen.
    hmm. well thats why i put the question mark in the subjec
    ive had it work in a variety of situations.. (including java). maybe its linux specific? can anyone shed some light on the origin of this escape sequence?

  4. #4
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Yup did a little googling and it appears to be linux only.

    Although it would be interesting to try it on cygwin though....
    What is C++?

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    There is no standard way to clear the console as the language itself has no concept of a console. It knows files and streams and such but doesn't know about a console.

  6. #6
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    Quote Originally Posted by Thantos
    There is no standard way to clear the console as the language itself has no concept of a console. It knows files and streams and such but doesn't know about a console.
    yeah, i know the language has no standard. I though it might have been a standard accross consoles. Guess that was a long shot

  7. #7
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    The only thing that I can think of that would do that is to just fill the screen with newline characters, but I seriously doubt that would work very well.
    What is C++?

  8. #8
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Quote Originally Posted by Vicious
    The only thing that I can think of that would do that is to just fill the screen with newline characters, but I seriously doubt that would work very well.
    No it wouldn't since you don't know the exact number of lines on the console. And of course it'd urk someone to no end if they redirected the output to a printer

  9. #9
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> can anyone shed some light on the origin of this escape sequence?
    It's the VT100 control sequence for "reset to initial state".

    The FAQ covers most of the ways one can clear the screen.

    And "cout << flush;" is the c++ way of saying "fflush(stdout)".
    "endl" will also flush the stream after placing a newline in it.

    gg
    Last edited by Codeplug; 09-10-2004 at 07:39 AM.

  10. #10
    Registered User Frobozz's Avatar
    Join Date
    Dec 2002
    Posts
    546
    Someone should write a library for this that has methods for the most popular operating systems and then stick it to the top of this forum.

    One other problem I just thought about concerning newlines is it might flicker quite a bit.

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Someone should write a library for this that has methods for the most popular operating systems and then stick it to the top of this forum.
    Code:
    #include <iostream>
    
    void clear_screen()
    {
      std::cout<<"Silly kid, consoles are for programmers."<<std::endl;
    }
    My best code is written with the delete key.

  12. #12
    Registered User Frobozz's Avatar
    Join Date
    Dec 2002
    Posts
    546
    You make an interesting point. In the past all I've seen the console used for is debugging applications.
    Last edited by Frobozz; 09-10-2004 at 10:51 AM.

  13. #13
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >In the past all I've seen the console used for is debugging applications.
    That wasn't really my point. The point was that if you want to clear the screen, shouldn't you be using a GUI in the first place? Clearing the console is very rude because unless your process owns it, there's likely to be output from other programs prior to yours. By clearing the screen you would be erasing that output and the result would be at least one irritated user.

    Clearing the screen (as most people want to do it) goes against all guidelines for good command line interface design. So maybe a more accurate function would be:
    Code:
    #include <iostream>
    
    void clear_screen()
    {
      std::cout<<"Use a GUI!"<<std::endl;
    }
    My best code is written with the delete key.

  14. #14
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >Clearing the console is very rude because unless your process owns it
    Unless you are making a game.

  15. #15
    Registered User Frobozz's Avatar
    Join Date
    Dec 2002
    Posts
    546
    The game idea is what I was thinking of. If what I was making wasn't a game, I wouldn't even touch the console.

    Actually I like the technique used in the Win32 version of Megazeux. In that the developer used SDL to emulate the console since most of Megazeux uses specialty features not capable of being done with the Win32 console.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Console window won't clear past app
    By LLINE in forum C++ Programming
    Replies: 4
    Last Post: 03-09-2009, 06:01 AM
  2. Console, Terminal and Terminal Emulator
    By lehe in forum C Programming
    Replies: 4
    Last Post: 02-15-2009, 09:59 PM
  3. One process with two console windows
    By siavoshkc in forum Windows Programming
    Replies: 8
    Last Post: 01-30-2009, 04:13 PM
  4. Replies: 12
    Last Post: 05-06-2006, 03:34 PM
  5. Using a button to clear a list box!
    By Unregistered in forum C++ Programming
    Replies: 13
    Last Post: 08-23-2002, 07:44 PM