0

This is my first post. How do you clear the screen in a console application written in C++? Please understand I don't want to use any extra preprocessors. Would have to do:

cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n";

Could I do that, is there a more professional way of doing this?

  • 1
    There is no standard way to do this. At best you're choices can be normalized into a multi-platform library like [ncurses](http://en.wikipedia.org/wiki/Ncurses). Obviously system-dependant solutions will both work, and be numerous. – WhozCraig Mar 07 '14 at 10:02
  • const int ROWS_ON_SCREEN = 50; const string CLEAR_SCREEN('\n', ROWS_ON_SCREEN); this is a bit tidier. Then you can call cout << CLEAR_SCREEN; – Graham Griffiths Mar 07 '14 at 10:07
  • 1
    Related question: [How do i clear the console in both Windows and Linux using C++ ?](http://stackoverflow.com/questions/228617/how-do-i-clear-the-console-in-both-windows-and-linux-using-c) – WhozCraig Mar 07 '14 at 10:09
  • @GrahamGriffiths You do not want to do that. You have to remember that iostreams etc. can be redirected and that is a poor solutuion. You do not want to rely on behaviour that could cause issues if instead of printing to console he changed to a file etc. – const_ref Mar 07 '14 at 10:11

2 Answers2

4

In pure C++ you cannot since C++ doesn't even have the concept of a console. You could essentially be printing to anything(file, printer, dashboard) or even redirecting to another program etc.

It therefore depends on the OS or relies on you using a Library such as ncurses

In windows, for example, you can do the following

#include <stdlib.h>

int main(int argc, char* argv[])
{
  system("cls");
  return 0;
}
const_ref
  • 4,016
  • 3
  • 23
  • 38
  • Hmmm, I'll do that. Thanks. :) I have one more question, someone told me to avoid using the Library that allows you to do things like system("pause"), system("cls"), and so on. Why is that? Isn't something to do with compatibility and security? And isn't that just like in command prompt? Or does the library allow you to use command prompt? – Lee360TheCoder Mar 07 '14 at 10:38
  • Yes using system sends the call to the OS and they decide what to do with it so for both compatabilty i.e. will the OS be able to handle this call and security reasons its not always an ideal solution. For small apps i.e. uni projects etc its not a real issue – const_ref Mar 07 '14 at 10:47
  • 1
    @const_ref Except when your uni hangs you if you dare using it. –  Mar 07 '14 at 11:00
  • 1
    @Mayerz haha well yes that would be a problem. YMMV! – const_ref Mar 07 '14 at 11:02
  • They learned us it was always bad practice, instead you can use [execve](http://manpagesfr.free.fr/man/man2/execve.2.html), but it's not as easy to use :) –  Mar 07 '14 at 11:08
  • @Mayerz Nothing is always "bad practice". There is a time and place for these things as long as you take necessary precaution in their use. I would not use it in released code in work etc, but its good to know that it can be used and any pitfalls associated, as well as any alternatives. – const_ref Mar 07 '14 at 11:11
  • Yep you'r right. Found some interesting stuff about it [here](http://stackoverflow.com/questions/1697440/difference-between-system-and-exec-in-linux/1697454#1697454) –  Mar 07 '14 at 11:14
0

too simple depending on your OS.

on linux use >

 system("clear");

on windows use >

system("cls");

for cross platform app you can use both together. It would not harm your program as such.