How can I clear the text of a console window?
WP
Printable View
How can I clear the text of a console window?
WP
If you're using Borland you can use the clrscr() function, otherwise you could print however many newlines as there are lines on the screen. I don't remember how many that is.
I'm using C++, so clrscr() doesn't exist.
What you said about inserting a lot of empty strings, does clear the screen, but then when I insert normal text again, it comes at the bottom of the console, and not at the top.
Any other suggestions?
WP
Try this, put this in your iostream.h file:
#if defined(BORLAND_C)
#include <conio.h>
#define cls() clrscr()
#endif
#if !defined(BORLAND_C)
#include <stdlib.h>
#define cls() system("cls");
#endif
Then whenever you want to clear it, just do cls().
It's never a good idea to alter the system headers. Put it into a header file of your own if you need to use it. Also, this is slightly more elegant:
If the files are included unnecessarily it won't make any difference because anything that's not used will be removed by any optimising compiler anyway.Code:#include <conio.h>
#include <stdlib.h>
#if defined(BORLAND_C)
#define cls() clrscr()
#elseif
#define cls() system("cls");
#endif