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

+5

Can you clear terminal in c++

I want to draw to the terminal and then clear it

c++

8/26/2021 3:28:04 PM

Bob

14 Answers

New Answer

+8

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

+6

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.

+4

You can use ansii escape codes . For clearing screen it will be \x1B[2J. So cout << "\x1B[2J" More on them, https://gist.github.com/fnky/458719343aabd01cfb17a3a4f7296797

+2

A thread about conio.h https://www.sololearn.com/Discuss/1947816/?ref=app Edit: A discussion thread about turbo c++ that some might find helpful https://www.quora.com/What-is-the-difference-between-using-Turbo-C-and-GNU-C-C++-Is-there-any-real-difference-or-if-I-submit-the-C-file-would-that-be-the-same-thing?top_ans=17291089

+2

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.

+2

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. https://www.cplusplus.com/forum/articles/11153/

0

Thanks

0

It says I can't include conio.h

0

No

0

Just sololearn. My computer is currently downloading vocode for cpp

0

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

0

The solution worked for me thanks to the community and the members for the solution. https://www.paycheckrecords.me/

0

-4

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