# Clearing the screen on a console program

## Recommended Posts

viscerante    130
Does anyone know how to clear the screen on the console. I am looking for something similar to the QBASIC command CLS. I am using C++ and Microsoft's visual C++ 6.0. Right now I am working on text base games, but want a fresh screen for each action. Any help is greatly appreciated. Vis.

##### Share on other sites
Xiachunyi    556
#include <stdlib.h>...system("cls");

I think that is the right header file.

##### Share on other sites
Well, there are different ways to do this.

One way you could do this, but I wouldn't advice because I think it is platform independent and is not used very much at all is system("cls").

The way I would this is like this:

for(int i = 0; i<30; i++){    cout<<"\n";}

Hope that helps.

##### Share on other sites
viscerante    130
Thanks Xiachunyi, that works perfectly!!

Vis

##### Share on other sites
Quote:
 Original post by Chad SmithWell, there are different ways to do this.One way you could do this, but I wouldn't advice because I think it is platform independent and is not used very much at all is system("cls").The way I would this is like this:*** Source Snippet Removed ***Hope that helps.

From what I can see that just calls 'endl' 30 times. That would just toss everything to the top of the console out of view, wouldn't it? Unless I'm missing something...

 It does do that; well, 29 times not thirty. I don't think that'll work for the OP in a text game, otherwise your text would be only at the bottom of the console. Unless theres a way to scroll the console screen down or up.

##### Share on other sites
lol, it would be nice if I would actually read VERY carefully on the OP post. lol. Then, I would have saw that he was doing a Text Game. Yeah, on a text game you would most likely not want to do that. I guess that is what I get for programming the whole day on my Engine. Lol. My eyes get tired. Wow, I need to get something to drink.

PS: Just throw my idea away for text based games.

##### Share on other sites
Drew_Benton    1861
As an alternative to the system("cls"), which I don't mind using myself, this following function should do the same. It is take from this codeguru article:

//// Clears the screen//#include <windows.h>void clrscr(){  COORD coordScreen = { 0, 0 };  DWORD cCharsWritten;  CONSOLE_SCREEN_BUFFER_INFO csbi;  DWORD dwConSize;  HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);  GetConsoleScreenBufferInfo(hConsole, &csbi);  dwConSize = csbi.dwSize.X * csbi.dwSize.Y;  FillConsoleOutputCharacter(hConsole, TEXT(' '),                             dwConSize,                             coordScreen,                             &cCharsWritten);  GetConsoleScreenBufferInfo(hConsole, &csbi);  FillConsoleOutputAttribute(hConsole,                             csbi.wAttributes,                             dwConSize,                             coordScreen,                             &cCharsWritten);  SetConsoleCursorPosition(hConsole, coordScreen);}

I tried it out and it works well. The reason you would opt to take this approach over that of the system("cls") is because if you have a program such as ZoneAlarm, 'system' function calls will trigger it and might give people the wrong impression if they see their firewall trying to block an attempt for your program. There are probabally other reasons as well, but that's the best one I can think of why you'd want to use all of that over one line. That and having to include windows.h to use it. Ehhh, just an after thought, you are under no obligcation to use it if you do not want to [wink]

##### Share on other sites
discman1028    212
When I did text-based games, the first game I made (Connect-4 clone) cleared the screen completely, and then displayed the new state of the "board" with ASCII art.

However, Windows allows you to simply overwrite areas of text on the console, find the mouse position, etc, which I found to be very useful.

Here

I made an ASCII art drawing program using this. If you'd like the source, I'll post it here.

##### Share on other sites
Captain P    1092
By using system("cls"), it may be good to use cout << flush; before that, to make sure there's nothing left in the cout stream.

As for resetting the cursors position, I once found this function (uses windows.h):

void level::gotoxy(int x, int y){	HANDLE hConsoleOutput;	COORD dwCursorPosition;	dwCursorPosition.X = x;	dwCursorPosition.Y = y;	hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);	SetConsoleCursorPosition(hConsoleOutput,dwCursorPosition);}

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account