Can you clear terminal in c++ | Sololearn: Learn to code for FREE!


Can you clear terminal in c++

I want to draw to the terminal and then clear it


8/26/2021 3:28:04 PM


14 Answers

New Answer


// use this snippet it must work on all major platforms #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) || defined(_WIN64) void clear(){ system("cls"); } #else void clear(){ system("clear"); } #endif // now to use it just call clear() like this int main(){ cout<<"hello "; // assuming you have included libs clear(); cout << "world"; } // a side note : because Sololearn doesn't use real terminal .. dont expect it to clear the screen... no you cant clear the screen of sololearn in anyway ( untill you use some kinda buffer to temporarily store the state and flush when you are done) the real reason is provided by Martin Taylor sir.. for c/c++ it dont even know what a terminal is.. its none of its business..


C++ has no concept of a terminal screen. In C++ cout is an output stream not an array of character cells. In fact cout may not even be connected to a terminal at all. Consequently there are no standard functions to clear a screen. MS-DOS compilers, such as Turbo C, provided dos specific headers such as conio.h to do this. Under Microsoft Windows we use Windows API calls while Linux uses either the termcap libraries or the curses library. Do not assume that raw escape codes will work as different terminals, or terminal emulators, use different escape codes. That's why the termcap library was invented, its name is a contraction of "terminal capabilities" and it maps symbolic names to escape codes for different terminal types.


You can use ansii escape codes . For clearing screen it will be \x1B[2J. So cout << "\x1B[2J" More on them,


A thread about conio.h Edit: A discussion thread about turbo c++ that some might find helpful


Harshit Jawla conio.h is from an ancient complier which was never or has been the part of C++ standard libraries, so stop recommending Turbo C headers and it's functions to others here.


Prashanth Kumar, a terminal is the physical or virtual (emulated) screen on which stdout is displayed. C++ makes no assumption about its capabilities such as number of lines, number of columns, colours etc. Modern operating systems use virtual terminals and can emulate different types of legacy terminals such as vt100, vt200, xterm, ansi, etc. Each of these terminals behaves differently and use different escape codes. The dos terminal traditionally did not emulate any type of standard terminal. It used its own ms-dos interrupts and bios functions to manipulate the display. An optional ANSI.SYS driver was available to emulate an ANSI terminal. The Windows cmd prompt uses Windows API calls. Using system() to clear the screen is a bad idea and opens a security hole a mile wide. See "Why system() is evil" on the C++ forum.




It says I can't include conio.h




Just sololearn. My computer is currently downloading vocode for cpp


Rellot's screwdriver conio.h complies to Microsoft visual studio 2019. I don't think that's ancient


The solution worked for me thanks to the community and the members for the solution.



#include <conio.h> now use clrscr() to clear the console