How to clear console in C++? | Sololearn: Learn to code for FREE!


How to clear console in C++?

Is there any way other than system ("CLS"); to clear the running console? In my Linux, it is saying "CLS not found" and then crashed.

4/13/2019 1:03:58 AM


10 Answers

New Answer


On windows, the command for clearing the console is 'cls'. The equivalent command on unix-based operating systems is 'clear'. So to clear the screen, use system("clear");


From now i will use that OS independent function


Kinshuk Vasisht woah! I didn't knew that thanks for the knowledge , I will try not to use that from now on :) And I sometime I just use cout<<"\x1B[2J\x1B[0;0f"; to clear the screen


Kinshuk Vasisht Okay now I think I should do some programming in windows too XD


include the header <conio.h> by writing #include <conio.h> then you can use the function clrscr() to clear the screen but it wont work on Mac


Also try not to use the system() function because that just invokes a command in the OS shell So in simple words It makes a program not able to run properly on all OS


Kinshuk Vasisht Can you also tell me some alternatives for getch() , kbhit() and gotoxy()?


Flaming Arrow That is part of the ANSI escape sequences, which are also OS dependent and are usually supported by unix-based systems. Windows does not support them though.


Flaming Arrow Using conio.h is deprecated as it is no longer supported by any of the modern compilers like GCC (or the windows port MinGW) or Clang supplied alongside IDEs like CodeBlocks. The system call is OS dependent, agreed, but the function is atleast part of the standard, unlike conio.h, which was only supplied with the Borland C++ Compiler. And you can design a generic version for multiple OS like this: void clear_screen() { #if defined(WIN32)||defined(_WIN32) system("cls"); #else system("clear"); }


it's system("cls");