How to clear screen without system("clear")

How to clear screen without using system("clear")? A solution is std::cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" << std::endl;, but I am making an encryption program, so I need to properly clear the screen.

Thanks in advance.
When I use std::printf("%c[2J",27); it does like printing '\n' many times. I need to properly clear the screen, like calling clear from the terminal. (Please don't make me go into POSIX stuff, because I have no idea what is a thread and these things. Only thing I know from POSIX is if(getuid()) // then i am not sudo ).

EDIT: I googled and most solutions use (n)curses.h. I tried that, but doesn't work. I can't input any text at all.

Thanks in advance.
Last edited on
Please post a solution
Not many will do this, also did you check out the post LB made?
did you check out the post LB made

Yes, I did. cout << string(100,'\n'); is a pathetic way, but I have no idea what the POSIX method does. Is there an explanation for that method?

Please post a solution
Not many will do this

Sorry for that, I wanted to say is it possible to do this without POSIX and (n)curses?
Last edited on
Nope, C++ doesn't (and isn't supposed to) know what a "console" or "terminal" is. All it knows is that it has an input stream and an output stream to connect it to the outside world.
I wrote this quick one up to handle it for me. It uses the native aplication programming interface:

https://gist.github.com/BeenEncoded/8cb1b1b89ee46171fbec

Also, to extend upon LB's response:

C++ wasn't made for any specific purpose. It's design allows it to take input, and produce output through stdin and stdout (cin and cout in C++). How a terminal handles this is generally to allow the user to enter a string for cin, and display anything out of cout. cin/cout can be routed to anything though: files, devices, etc...

If you programatically redirect stdout and stdin, you can have a program that takes input/ouput from a file, which is useful if you want another program to be able to "communicate" with it by sending input and recieving output.
Last edited on
Topic archived. No new replies allowed.