How to clear console in C++? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 1

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.

13th Apr 2019, 1:03 AM
Nafis
Nafis - avatar
10 Answers
+ 4
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");
13th Apr 2019, 2:10 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 6
From now i will use that OS independent function
13th Apr 2019, 2:04 PM
Shahil Ahmed
Shahil Ahmed - avatar
+ 5
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
13th Apr 2019, 1:59 PM
Shahil Ahmed
Shahil Ahmed - avatar
+ 5
Kinshuk Vasisht Okay now I think I should do some programming in windows too XD
13th Apr 2019, 2:03 PM
Shahil Ahmed
Shahil Ahmed - avatar
+ 3
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
13th Apr 2019, 1:38 PM
Shahil Ahmed
Shahil Ahmed - avatar
+ 3
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
13th Apr 2019, 1:40 PM
Shahil Ahmed
Shahil Ahmed - avatar
+ 3
Kinshuk Vasisht Can you also tell me some alternatives for getch() , kbhit() and gotoxy()?
17th Apr 2019, 8:19 AM
Shahil Ahmed
Shahil Ahmed - avatar
+ 2
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.
13th Apr 2019, 2:03 PM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 1
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"); }
13th Apr 2019, 1:45 PM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
0
it's system("cls");
13th Apr 2019, 2:00 AM
Juan David Padilla Diaz
Juan David Padilla Diaz - avatar