Skip to content
← Back to Community
Is it possible to clear the screen?
Profile icon
GitOverHere

Is it possible to clear the screen in repl.it. I want to be able to clear the screen after the player has entered some input.

Answered by mat1 [earned 5 cycles]
View Answer
Voters
Profile icon
EricLi28
Profile icon
GitOverHere
Comments
hotnewtop
Profile icon
mat1
Yes, it is possible. Simply do std::cout << "\e[2J\e[H" to clear the screen.

Printing that string will also work in almost every language on Repl.it too.

Explanation

It uses ANSI escape codes, which are special codes that you can print to the terminal to do certain things. For example, here we're printing \e[2J and \e[H. \e means an escape character in c++, which is what all ansi escape codes start with. Then, the [ character signifies that it is a CSI (Control Sequence Introducer) code. The characters proceeding it are the code for the sequence. The 2J means to clear the entire screen, but not move the cursor. Then, the H moves the cursor to the start of the terminal.
Please mark this comment as the answer if it answered your question. Thanks!

Profile icon
ash15khng

@mat1 Wow, this is cool. So this should work for most other languages?

Profile icon
mat1

@ash15khng Yup, for some languages you might have to replace \e with \033 though.

Profile icon
ash15khng

@mat1 Oh, I'm more familiar with that syntax.

Profile icon
EchoCoding

Yes! Import the library header file <stdlib.h> and use system("CLS") to clear the terminal.

Profile icon
MichelProgamer

Alguem tem um codigo clear, Via Node?

Profile icon
[deleted]

You could output a heap of newlines.

void clearScreen(int numLines) { for (int i = 0; i < numLines; i++) { cout << "\n"; } } // Call the function clearScreen(40);
Profile icon
PYer

not is c++. sorry. You could try to print 50 new lines, to make all the text scroll up.
Please mark this comment as the answer. Thanks!