Clear console screen

Hi all! I want to make a console game in C++ so I'm trying some stuff out with SetConsoleCursorPosition();

Now I've this test script to show the problem:

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
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <windows.h>
#include <iostream>
#include <ctime>
#include <time.h>
#include <stdlib.h>
using namespace std;

void goToXY( int column, int line )
{
	COORD coord;
	coord.X = column;
	coord.Y = line;
	SetConsoleCursorPosition(
	GetStdHandle( STD_OUTPUT_HANDLE ),
	coord
	);
}


void main()
{
	int startTime;
	int randomValue;
	COORD p;
	p.X = 1;
	p.Y = 1;
	srand((unsigned)time( NULL ) );
	while (true)
	{
		randomValue = rand() % 4 +1;

		switch(randomValue)
		{
		case 1:
			p.X++;
			break;
		case 2:
			p.Y++;
			break;
		case 3:
			p.X--;
			break;
		case 4:
			p.Y--;
			break;
		}
		goToXY(p.X,p.Y);
		cout << "#";
		Sleep(1000);
	}
}


Now, what I want is that the 'previous' printed # disappears. How can I do this? Clear the whole screen? In that case: how? Or is there another way?

Greets
Let me phrase it like that - to say that a console is a less than ideal environment for game development would be somewhat of an understatement. This was an understatement.

At least use ncurses or something like that - that will give you the functionality you need (and more) without binding you to windows.

http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/intro.html
Of course I know that console is not ment to be used for games. But I'm just a C++ beginner so I just want to try things like x,y movements out.
The thing is, because C++ doesn't have the concept of "outputting to a console" - only "put stuff into a stream" - things like "clear the console window" aren't there by default in C++ (stdout might be a file stream, or a stream that sends output to a printer etc). So you'd have to use third party libraries (like you're using the WinAPI function SetConsoleCursorPosition for your gotoxy). So if you wanted to do console stuff that already requires third party libraries, ncurses is the logical conclusion.
Okay, then I'll try it an other way. Thanks for your help.:)

ncurses only works for Linux and I'm using Windows. But i read something about Allegro, i'll try that. :)
Huh? No, there's at least one ncurses implementation for windows on gnuwin32's sourceforge page. Allegro isn't meant for consoles either, it's a library for low level gaming routines (like graphics, sound, input etc).
@Oneday

Here is a small program I found that clears a console window. It should be easy enough to incorporate it into whatever program you're creating.

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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Clearing The Screen.cpp : main project file.

#include <stdafx.h> // Used with MS Visual C++ Express. Leave off if using different compiler
#include <stdio.h>
#include "conio.h"
#include <iostream>
#include <windows.h>

using namespace std;

void cls( HANDLE hConsole )
{
   COORD coordScreen = { 0, 0 };    // home for the cursor 
   DWORD cCharsWritten;
   CONSOLE_SCREEN_BUFFER_INFO csbi; 
   DWORD dwConSize;
// Get the number of character cells in the current buffer. 
   if( !GetConsoleScreenBufferInfo( hConsole, &csbi ))
   {
      return;
   }
   dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
   // Fill the entire screen with blanks.
   if( !FillConsoleOutputCharacter( hConsole,        // Handle to console screen buffer 
                                    (TCHAR) ' ',     // Character to write to the buffer
                                    dwConSize,       // Number of cells to write 
                                    coordScreen,     // Coordinates of first cell 
                                    &cCharsWritten ))// Receive number of characters written
   {
      return;
   }
   // Get the current text attribute.
   if( !GetConsoleScreenBufferInfo( hConsole, &csbi ))
   {
      return;
   }
   // Set the buffer's attributes accordingly.
   if( !FillConsoleOutputAttribute( hConsole,         // Handle to console screen buffer 
                                    csbi.wAttributes, // Character attributes to use
                                    dwConSize,        // Number of cells to set attribute 
                                    coordScreen,      // Coordinates of first cell 
                                    &cCharsWritten )) // Receive number of characters written
   {
      return;
   }
   // Put the cursor at its home coordinates.
   SetConsoleCursorPosition( hConsole, coordScreen );
}

void Pause()
/* Wait for a return key to continue */
{
char YN;
cout << "\n\n\t\t<---  Please press 'Enter' to continue... --->";
while ( (YN = getchar() ) != '\n') ; /* Just wait for Enter key  */
return;
}  /* End of Pause() */

int main( void )
{
    HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
	for (int y = 0;y<1520;y++)
		printf( "X" );
	Pause();
    cls(console);
	cout << "\n\n\t\t";
    return 0;
}
Topic archived. No new replies allowed.