struct and SetConsoleCursor

When I step through my program using VS13 debugger it works correctly but when I let the program run on its own it only outputs my 'x' to the console completely leaving out my '*'. I am making 'tron' using only console and conio.h header. I think it has to do with how I am pointing to my class or something? I am still new with pointers and HANDLE,COORD,SetConsoleCursorPosition() so please tell me what I am doing incorrectly I know it has to do with something simple that I overlooked. ie. structure,pointer need destroy? etc..

Only the code being executed at this time of my program is shown.

struct and window sizing.
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
 //struct for player use
struct player
{	
	string name;
	char moveKey;
	char pos[30][100];
	int x = 0;
	int y = 0;

	void startpos(int *x, int *y)
	{
		srand(time(0));

		*y = rand() % 28 + 1;
		*x = rand() % 98 + 1;	
	}

	player()
	{

	}
}player1, player2, CPU;

void SetWindow(int Width, int Height)
{	
	
	_COORD coord;
	coord.X = Width;
	coord.Y = Height;

	_SMALL_RECT Rect;
	Rect.Top = 0;
	Rect.Left = 0;
	Rect.Bottom = Height - 1;
	Rect.Right = Width - 1;

	HANDLE Handle = GetStdHandle(STD_OUTPUT_HANDLE);      // Get Handle 
	SetConsoleScreenBufferSize(Handle, coord);            // Set Buffer Size 
	SetConsoleWindowInfo(Handle, true, &Rect);            // Set Window Size 
}


The actual function

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
void singleplayer()
{
	HANDLE sout = GetStdHandle(STD_OUTPUT_HANDLE);
	HANDLE sout2 = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD playt;
	COORD cput;

	player1.x = 0;
	player1.y = 0;
	CPU.x = 0;
	CPU.y = 0;
	char map[30][100];// sets map height and width. Memory mapping

	system("cls");

	theMap(map);// sends mapped array to draw map

	CPU.startpos(&CPU.x, &CPU.y);	
	cput.X = CPU.x; cput.Y = CPU.y;
	SetConsoleCursorPosition(sout, cput);
	cout << '*' ;

	player1.startpos(&player1.x, &player1.y);/*If you comment this line it works but doesn't play it at random instead at the static [0][0] pos*/
	playt.X = player1.x; playt.Y = player1.y;
	SetConsoleCursorPosition(sout2, playt);	
	cout << 'x' ;

	
}

void theMap(char cmap[30][100])//draws map
{
	for (int a = 0; a < 30; a++)
	{
		for (int b = 0; b < 100; b++)
		{
			cmap[a][b] = ' ';
			cmap[0][b] = '&';
			cmap[29][b] = '&';
			cmap[a][0] = '&';
			cmap[a][99] = '&';			
			cout << cmap[a][b];
		}
	}	
} // end the map 
Last edited on
Figured it out thanks for your help. -.-

It worked when I debugged and stepped the program because the system time seed for srand() was delayed enough to move the cursor to a different location.

When I ran the program without stepping it didn't have enough delay.

Simply put a Sleep(1000); so that the system clock changed time to create a new seed. -.-
Lol, no one even helped you.
Topic archived. No new replies allowed.