How do I clear screen in my c++ program? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 14

How do I clear screen in my c++ program?

Trying to create a tic tac toe game , needs the outdated clrscr() function in conio.h in standard version

14th Apr 2017, 2:26 AM
A$APcode
A$APcode - avatar
4 Answers
+ 10
Here's what I use. It's not the perfect solution, but it does the job: void clear() { #ifdef _WIN32 system("cls"); #else std::cout << "\033[2J\033[1;1H"; #endif } If you're curious why this works, see: https://en.wikipedia.org/wiki/ANSI_escape_code
14th Apr 2017, 2:33 AM
Squidy
Squidy - avatar
+ 13
If you're trying to clear console screen on C.Playground, I don't think there's a way, because it doesn't actually work like a desktop console. clrscr() is not working for me on desktop either, so I go for system("CLS"), but I believe that isn't a good practice either. One nasty way is to do multiple newlines using a loop to push previous output out of console display range. for (int i = 0; i < 39; i++, std::cout << "\n");
14th Apr 2017, 2:35 AM
Hatsy Rei
Hatsy Rei - avatar
+ 13
You may use system("cls") on a Windows Platform, or system("clear") on Linux - Based Consoles... system("clear") works on Android apps like CppDroid as well...
14th Apr 2017, 3:13 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
0
Guys, I need answer about this... How to use system("CLS") in cppdroid. I using cppdroid in android device. hoping I can solve this..
14th Oct 2018, 7:34 AM
Jefferson Engano
Jefferson Engano - avatar