• ### Announcements

#### Archived

This topic is now archived and is closed to further replies.

# console problem

## Recommended Posts

Hey, I have tried to find this many places and failed. I want to clear the contents of my console app''s window. Such as in dos when you type CLS. I tried the known command
  system("cls");
But it had no effect. Is there another way? It needs to be fairly fast so I can get a decent looking refresh rate for my ASCII based rpg. Thanks a ton to all who answer. Right now I''m stuck doing something equivilant to
  cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n...
Thanks again. Alex Broadwin A-Tronic Software & Design ----- "if you fail in life, you were destined to fail. If you suceed in life, call me." "The answer is out there." "Please help, I''m using Windows!"

##### Share on other sites
There are better ways, but this should do it ...
  DWORD numCharactersWritten;COORD consoleCoords = {0, 0};GetConsoleScreenBufferInfo(consoleHandle, &consoleInfo);FillConsoleOutputCharacter(consoleHandle, ' ', consoleInfo.dwSize.X * consoleInfo.dwSize.Y, consoleCoords, &numCharactersWritten);FillConsoleOutputAttribute(consoleHandle, consoleInfo.wAttributes, consoleInfo.dwSize.X * consoleInfo.dwSize.Y, consoleCoords, &numCharactersWritten);

... where consoleInfo is a CONSOLE_SCREEN_BUFFER_INFO, and consoleHandle is a HANDLE to a console.

Edited by - Martee on January 8, 2002 10:57:45 PM

##### Share on other sites
I don''t think this will work in my situation. This is the base of the console app, the skeleton code you begin with.

  #include using namespace std;int main( void ){ cout << "This is a test" ; return 0;}

I have no win32 at all, nothing using windows.h! So how would I clear this screen. I have only basic knowledge on console apps, as you probably can see.

A-Tronic Software & Design
-----
"if you fail in life, you were destined to fail. If you suceed in life, call me."
"The answer is out there."

##### Share on other sites
If you include windows.h, it should work. I used the above code in a similar program. But otherwise ... I don''t know of any non-Windows ways of doing it.

##### Share on other sites
You could just do a system call.

system("cls";

?

##### Share on other sites
I tried including windows.h, no luck. Thanks anyway. I decided to make it using win32 and win32 draw functions. I''m building my own basic cout with \n and so on. More flexible anyway I guess. Easy to port too. Thanks for your time.

A-Tronic Software & Design
-----
"if you fail in life, you were destined to fail. If you suceed in life, call me."
"The answer is out there."

##### Share on other sites
It''s a little ugly, but you should be using a console buffer anyway... this might help you on your way...

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/conchar_25b9.asp

D

##### Share on other sites
is it win32 or dos based.

If it is not a win32 exe (if your lucky you can still pull it off) jsut #include <conio.h> and use clrscr();

there are equivilent functions and workarounds in stdio, but I forgot what they were as I always used conio.

##### Share on other sites
win32 exe I believe. As usual, an emulated console.

A-Tronic Software & Design
-----
"if you fail in life, you were destined to fail. If you suceed in life, call me."
"The answer is out there."

##### Share on other sites
did you included cstdlib?
If you did what happened with system ("cls");

##### Share on other sites
as my first post says, absolutely nothing.

A-Tronic Software & Design
-----
"if you fail in life, you were destined to fail. If you suceed in life, call me."
"The answer is out there."

##### Share on other sites
I''m not sure, but I think that Win32 consoles may support ANSI escape codes, because I seem to remember porting a really old BBS door game I wrote to Win32. There should be a code to clear the screen. Search the net for ANSI escape sequences and you''ll probably be able to find something useful.

##### Share on other sites
I''m not sure why that happened and there are no compilers around here so if this doesn''t work, sorry. Try doing a cout << flush;
before the system("cls");. Because the buffer may be flushing after the call. Also i think that there is an exec function maybe it works there..

By the way there is the posibility of your compiler having conio.h, if you do there is a function call clearscreen or cls or something like that, read the header

##### Share on other sites
Found what I needed in conio.h, thanks a ton. One other thing, anyone know how to input a single char without waiting for the user to hit return?

A-Tronic Software & Design
-----
"if you fail in life, you were destined to fail. If you suceed in life, call me."
"The answer is out there."

##### Share on other sites
using C++ iostream of C IO functions? I personally enjoy the simplicity of some older C functions.

##### Share on other sites
getch(). Cant remember what header its in though, its either conio.h or stdio.h try em both

Thanks a ton!