Making a function that clears the console

Hello there guys, I am trying to make a function that clears the console. I just graduated high school some months ago and in October I will go to university to study informatics (computer science). I am telling you this because in high school I was taught that using system("cls"); is the way to go, even though the teachers never told us what the system command does. Later, while browsing the web and looking for some documentation on the matter I found out that is not a very efficient way to do it (and right now I am working on a project that really doesn't need the mess the system command brings to the table).
I even read some of the alternative methods, and even though some seemed quite simple, I thought they were ridiculous (like filling the window with newline characters, eww...), and some that were more practical, really made me lose my mind trying to decipher them (cus of the amount of advanced coding in there). I know that some might say it's better to stick to system("cls"); for now, but I really want to know if there is any way, more beginner-friendly (or at least that does not require any really advanced skills) to do it.

The current function I use:
1
2
3
4
5
  void WinRefresh()
{
    system("cls");
    std::cout << "" << std::endl;
}
system() just calls an operating system command. By their very nature they are operating-system dependent and if you are very naughty then you can do bad things with them. But, if you're not naughty, it's a good way of clearing the screen.

If you were in the command window, then cls would be the command to clear the text from that command window. Nothing more, nothing less.

Apropos of nothing, I don't know why you need line 4.
It's slow because a system() call does something along the lines of:
- interrupting the calling program
- have the OS parse the command (as a string)
- have the OS decide to spawn another process or interact with your process/console
- give control back to the calling program

The alternative would be to directly make Win32 calls to clear your console screen. But that is more involved than a simple system() call.

If the performance of the screen clearing isn't an issue for you, then don't worry about it. It's a very minor thing to be concerned with in most cases.
Last edited on
Everybody wants to clear the screen at some stage in their programming career and just as many people say you shouldn't do it, all for no conceivable reason other than they just like dispensing no/don't-pills.

The long answer close at hand: https://www.cplusplus.com/articles/4z18T05o/#POSIX

The short answer: system('cls') is good enough - fill yer boots and clear at will - just don't(?) use abrasives.

'naughty' ... bizarre
In the most benign case, clearing the screen simply prevents the user from reading the output of prior commands. It's typically a bad idea from a usability standpoint.

There's hardly any reason to clear the screen unless you're writing an interactive program, which is typically non-ideal.

The vast majority of console programs are intended to be used noninteractively. Once such a program is started on its task, it runs to completion without ever stopping to ask for input from the user. There are major advantages to this style of operation, but to really understand what this means and how it affects the design of decent console programs, you'll need to become familiar with the command line yourself.

Alternatively, ESR writes about it extensively here:
http://www.catb.org/~esr/writings/taoup/html/
Which is a worthwhile and easy read. If you do read it, keep in mind that ESR is a very opinionated guy.
@OP wants to know how to clear the screen and not that it's against some rule that nobody cares about which interferes even to the extent of ridiculous commandments they are mis-using their property and even the software intentions, albeit completely imagined.

I think we should write a strongly worded letter, straight away, to all the OS developers, and chip manufacturers/developers, perhaps even Bjarne and demand they remove the capability. They have taken a complete liberty including it in system("cls") in the first place.

Naughty Lives Matter!
As Windows 10 now supports VT100 for the console, the VT100 escape sequence can be used to clear the console. For windows consider:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <stdio.h>
#include <windows.h>

void cls()
{
    if (const HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); hOut != INVALID_HANDLE_VALUE)
        if (DWORD dwMode; GetConsoleMode(hOut, &dwMode))
            if (SetConsoleMode(hOut, dwMode | ENABLE_VIRTUAL_TERMINAL_PROCESSING))
                printf("\x1b[H\x1b[2J");  // Home and clear screen
}

int main()
{
    cls();
}


The VT100 escape control codes are explained at http://ascii-table.com/ansi-escape-sequences-vt-100.php

For info about VT100 and the console, see https://docs.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences

Last edited on
Topic archived. No new replies allowed.