I'm currently clearing my console window with this piece of code:

void clrScr()
    COORD cMap =
        0, 3
    if(!FillConsoleOutputAttribute(hCon, 0, 2030, cMap, &count))
        std::cout << "Error clearing the console screen." << std::endl;
        std::cout << "Error code: " << GetLastError() << std::endl;

, which I call once in the main loop.
But since my window is quite large (70x35), it's flickering quite a bit.

I was wondering if there are any faster methods of doing this?

  • 2
    \$\begingroup\$ Have a look at ncurses for a platform neutral techniques for clearing a console window. \$\endgroup\$ – Martin York Dec 4 '12 at 7:33
  • \$\begingroup\$ Not really looking for a new lib. Thanks anyways. \$\endgroup\$ – user19953 Dec 4 '12 at 9:57
  • 3
    \$\begingroup\$ You should be looking for a new lib. You are using standard IO for stuff which it wasn't meant to be used for. \$\endgroup\$ – Winston Ewert Dec 5 '12 at 21:27

Here is an idea; ANSI escape codes work on Windows, Linux and OSX:

cout << "\033c";

\033 - stands for octal ESC, c - resets the device (terminal is default) to the initial state ( clear the screen,clear the buffer also so it is not possible to scroll, reset the fonts and so on).

In some cases it may be more useful than well known which simply adds some empty lines and puts the cursor in the upper left corner:

cout << "\033[2J\033[1;1H"

Have a look at Clear the Screen. It basically boils down to using what you did (since the Windows console doesn't accept standard ansi sequences), although you might try the conio.h route.

  • \$\begingroup\$ I can't use clrscr() for some reason. Probably something to do with the fact that it's non-standard. And pretty much the only other way (without using external libs) would be the way I'm doing it.. It seems there are no better ways of doing this, so I won't accept an answer, at least for now. If there are no better answers after a while, I'll accept yours. \$\endgroup\$ – user19953 Dec 5 '12 at 23:09

I decided that the best way to clear the screen - at least for a text-based game in console - is to clear literally only the individual squares that need to be cleared, instead of the whole window.

  • 1
    \$\begingroup\$ Which is indeed almost the right and proper way to handle it in a game - if only you'd told us that :) But you could try overprinting with the new contents rather than clearing first. \$\endgroup\$ – Glenn Rogers Dec 6 '12 at 7:13

How about


This clears the entire console nicely

  • 6
    \$\begingroup\$ That is actually WAY slower than my method. Also, system calls are supposedly very bad coding practice. \$\endgroup\$ – user19953 Dec 4 '12 at 2:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy