Array box not moving to coordination?

Created an array box of characters and added coordination from Windows.h. However only the top side of the box moves to the right while the rest just goes down in position and ends up separating.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void Box(int posX, int posY){
    COORD coord;
    coord.X = posX, coord.Y = posY;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord);
    char box[7][5]={
	{'\xdb','\xdb','\xdb','\xdb','\xdb',},
	{'\xdb',' ',' ',' ','\xdb'},
	{'\xdb',' ',' ',' ','\xdb'},
	{'\xdb',' ',' ',' ','\xdb'},
	{'\xdb',' ',' ',' ','\xdb'},
	{'\xdb',' ',' ',' ','\xdb'},
	{'\xdb','\xdb','\xdb','\xdb','\xdb',}
    };

    for(int rows(0); rows<7; rows++){
	for(int columns(0); columns<5; columns++){
	    std::cout<<box[rows][columns];
        }
        std::cout<<std::flush;
    }
}

You need to separate the screen positioning code from Box, and update the position of the cursor inside your FOR loop such as:

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

#include <iostream>
#include <Windows.h>

void Box(int posX, int posY);
void posCursor(int x, int y);

int main()
{
	// just a test code to move
	// across 10 times.
	for (int i = 0; i < 10; i++) {
		system("cls");
		Box(i, 10);
		system("pause");
	}
	return 0;

}

void Box(int posX, int posY){
	char box[7][5] = {
			{ '\xdb', '\xdb', '\xdb', '\xdb', '\xdb', },
			{ '\xdb', ' ', ' ', ' ', '\xdb' },
			{ '\xdb', ' ', ' ', ' ', '\xdb' },
			{ '\xdb', ' ', ' ', ' ', '\xdb' },
			{ '\xdb', ' ', ' ', ' ', '\xdb' },
			{ '\xdb', ' ', ' ', ' ', '\xdb' },
			{ '\xdb', '\xdb', '\xdb', '\xdb', '\xdb', }
	};

	for (int rows(0); rows<7; rows++){
		for (int columns(0); columns<5; columns++){
			posCursor(posX + rows, posY + columns);
			std::cout << box[rows][columns];
		}
		std::cout << std::flush;
	}
}

void posCursor(int x, int y)
{
	COORD coord;
	coord.X = x, coord.Y = y;
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
Last edited on
Ooh, wow, now I see what I was doing wrong. Thank you Softrix.
Your welcome :)
Topic archived. No new replies allowed.