system()

i just read an article why not to use system("cls")...but what is another alternative for it??i mean what else can i do to clear the screen???
i am using wxdev c++ on windows 7...
Perhaps you could output std::endl a hundred times.
Don't. There is no reason to clear a console screen. It's designed for input and output streams, not pretty graphics. If you want to code pretty graphics, learn a proper GUI package like Qt, wx, etc...

In Windows you can do the following

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  HANDLE hConsole = GetStdHandle( STD_OUTPUT_HANDLE );

  CONSOLE_SCREEN_BUFFER_INFO csbi;
  memset( &csbi, 0, sizeof( csbi ) );
  
  // get the number of character cells in the current buffer
  BOOL  bSuccess  = GetConsoleScreenBufferInfo( hConsole, &csbi );
  
  DWORD dwConSize = csbi.dwSize.X * csbi.dwSize.Y;

  DWORD cCharsWritten = 0;
  COORD coordScreen = { 0, 0 };

  // fill the entire screen with blanks
  bSuccess = FillConsoleOutputCharacter( hConsole, (TCHAR)' ', dwConSize, coordScreen, &cCharsWritten );

  // put the cursor at (0, 0)
  bSuccess = SetConsoleCursorPosition( hConsole, coordScreen );

  // Now the screen should be clear 
A "Window" aka "Rendering Context" aka "RenderWindow" aka "WNDCLASS" etc. are all constructs of the library you are using to interface with the output device. The current C++ standard actually doesn't require a window to be drawn but most libraries do it for you by default.

So the method you use to clear the window is library dependent. The Standard Library does not have a native method for the reasons that cnoeval pointed out.
Don't. There is no reason to clear a console screen. It's designed for input and output streams, not pretty graphics. If you want to code pretty graphics, learn a proper GUI package like Qt, wx, etc...

I guess according to you then there would of been no reason for early game developers to make games to run on the DOS OS. It wasn't meant to run games it was meant for serious business. :P

To the OP:
Binarybob's example works, but getting the return value of the functions with bSuccess and not doing anything with it is a bit silly. Here's a function that Duoas provided somewhere that I grabbed a good while back:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
void ClearScreen()
{
	HANDLE                     hStdOut;
	CONSOLE_SCREEN_BUFFER_INFO csbi;
	DWORD                      count;
	DWORD                      cellCount;
	COORD                      homeCoords = { 0, 0 };


	hStdOut = GetStdHandle( STD_OUTPUT_HANDLE );
	if (hStdOut == INVALID_HANDLE_VALUE) return;


	/* Get the number of cells in the current buffer */
	if (!GetConsoleScreenBufferInfo( hStdOut, &csbi )) return;
	cellCount = csbi.dwSize.X *csbi.dwSize.Y;

	/* Fill the entire buffer with spaces */
	if (!FillConsoleOutputCharacter(
	hStdOut,
	(TCHAR) ' ',
	cellCount,
	homeCoords,
	&count
	)) return;

	/* Fill the entire buffer with the current colors and attributes */
	if (!FillConsoleOutputAttribute(
	hStdOut,
	csbi.wAttributes,
	cellCount,
	homeCoords,
	&count
	)) return;

	/* Move the cursor home */
	SetConsoleCursorPosition( hStdOut, homeCoords );
}


And personally I say doing stuff like this with the console is fine when you're learning. It's definitely not what the console was designed for, but if it was never meant to clear itself then the command 'CLS' wouldn't exist (Go type that directly into a command prompt). Basically, if you want to do stuff like this for something serious you don't really want to rely on the console. But for messing around and making a simple game, it can be a pretty awesome way to learn.

I owe a good bit of my advancement in programming to trying to get the console to function in ways it wasn't really intended. For example, print out a border with a dimension of 54 cells wide by, say, 10 cells deep. Then try to figure out how you can get the console to print messages inside that box, and wrap them logically (based on the whole word, rather than cutting mid word). You have to program a lot of the functionality yourself, but most of it can be done with logic and doesn't require learning the ins and outs of a library. You can pretty much do this whole border/word wrap thing with SetConsoleCursorPosition(), strings, and some creative thinking.

Most importantly, that I feel anyway, is the fact that it makes you have to think a lot. Think about how you can get the console to do what you want it to when there are no functions to do so.
Last edited on
What is wrong with system("cls"); ?
What is wrong with system("cls"); ?


This isn't a good idea. Using this, you call a extern program, 3 process running... (Shell, your program and the cls).
@ andrezc: It's a common mistake that people make because this is usually why we discourage using "system()" but cls isn't actually a program, it's a command given to the shell which is actually what the "system()" function does and what it was intended for. You can verify this by going to Start -> Run and type "cls" it will return an error everytime and on every version of Windows. If it were a seperate App it would run then disappear.
Last edited on
What is wrong with system("cls"); ?


Because if I've used that PC before you, pause and cls no longer do what you want them to.
What is wrong with system("cls"); ?

Because I sometimes whack people over the head when they accidentally make their code platform specific. >_>

SMALL AMENDMENT: Or when they make their code platform specific due to the request of an instructor who should know better than to use system("cls").

-Albatross
Last edited on
I don't think many new programmers really care about platform dependency. I'd like to know how many new programmers go through the thought process of "I need to make sure this temperature converter remains platform independant" or "This Tower of Hanoi program would be significantly improved if I could use it on any platform".

Platform independancy isn't hard to learn or understand when you've got a good bit of progamming experience under your belt, so there's really no point in pushing it on new programmers. All it does is just force them to try thinking like a commercial programmer when they're still trying to learn the basics of a pointer. Most people who are serious about programming will figure it out on their own and on their own time.
..."This Tower of Hanoi program...

Are you implying that there are a lot of new programmers who think about the Tower of Hanoi problem? \:|

At any rate, you're right. I'll make a small amendment to my post.

-Albatross
It's just a common 'learn programming' thing I used for an example. xD

I completely agree with your amendment, however. At the very least if an instructor says to use system() they should also point out that it shouldn't be used for more serious projects. I mean, it makes sense for simplicitys sake and not overwhelming a beginner, but it should definitely not be taught with the mindset of 'Just use it, you'll be fine'.

My personal favorite would be programming books which sport example code using void main().
I guess according to you then there would of been no reason for early game developers to make games to run on the DOS OS. It wasn't meant to run games it was meant for serious business. :P


DOS != Console. Although I admit I used to love a game called Rogue which I think was actually run through the console, most of those games you're talking about would write their graphics to a video display driver through DMA. Only the text-based games used the console display (and they didn't bother to clear the screen.) 8^P
I actually made a game engine to run a text based rogue-like on the console. That was what I used the logical word wrapping for, game messages. When I made a small game out of the engine, I used clear screen once to make sure there wasn't anything on screen when the UI was drawn just incase it was started through the command prompt.

Pretty much everything about this engine violated 'normal' console operations.

My point was with my comment is that at one time computers weren't built with running games in mind what so ever, yet people made games anyway.
Use ncurses to clear the console screen, although most likely in your case there's no reason not to use system.

As to reasons to clear the screen, there are many. For example, Vim clears the screen...
Topic archived. No new replies allowed.