-
flush empties the output buffert and makes sure that whatever is in there it will be outputed (I think lol). This comes in handy when you really want to make sure everything in the output buffert will be printed to the screen for example. Also endl will call the same function aswell.
-
>is there a better way than this?
Yes, but you fall into the realm of platform and compiler dependent solutions with them.
-
Highly non-portable. :)
Code:
#include <windows.h>
void
clear(int left, int top, int right, int bottom, int fill = ' ')
{
int length = right - left;
COORD coord;
DWORD unused;
HANDLE screen = GetStdHandle(STD_OUTPUT_HANDLE);
for(coord.X = left, coord.Y = top; coord.Y < bottom; ++coord.Y)
{
FillConsoleOutputCharacter(screen, fill, length, coord, &unused);
}
}
void
clear(int fill = ' ')
{
CONSOLE_SCREEN_BUFFER_INFO info;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &info);
clear(0, 0, info.dwSize.X, info.dwSize.Y, fill);
}
-
If you using the Win32 console, try this code snippet I got from a book entitled "Game Programming All-In-One" (with a few modifications):
Code:
void clearscreen(void)
{
HANDLE screen = GetStdHandle(STD_OUTPUT_HANDLE);
COORD start;
DWORD written;
WORD forecolor = 7;
WORD backcolor = 0;
start.X = 0;
start.Y = 0;
FillConsoleOutputAttribute(screen, forecolor + (backcolor << 4), 80 * 25, start, &written);
FillConsoleOutputCharacter(screen, ' ', 80 * 25, start, &written);
SetConsoleCursorPosition(screen, start);
}
As a quick note, you'll need to #include <windows.h>.
Edit: Hmm... appears I should've looked to see if there was a second page. Oh well... at least mine doesn't leave the previous colors. :D