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

-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.

4/13/2019 1:03:58 AM

Nafis

10 Answers

New Answer

+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");

+6

From now i will use that OS independent function

+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

+5

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

+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

+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

+3

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

+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.

+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"); }

0

it's system("cls");