how to change value of array with displaying it multiple time

closed account (SiAR92yv)
Hey guys, so i am a beginner with c++ and i am trying to write a code that will help me move a person which is in this case 'X' in an array of 10x10 ... so this is my code. The problem i am facing is that i need to keep my array 10x10 displayed and start updating it with the position of the 'X' ... When i enter 'z' for example it needs to move up ... i tried to change it by position only but it didn t work . i am trying it for 'z' now only .
Thanks a lot and excuse my bad english .

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
#include<iostream>
#include<stdlib.h>
#include<time.h>
#include<iomanip>

using namespace std;

void remplir_tableau(char tab[10][10])
{
	for (int i=0;i<=9;i++)
	{
		for (int j=0;j<=9;j++)
		{
			tab[i][j]='-';
		}
	}
}

void afficher_tableau(char tab[10][10],int x, int y){
	tab[x][y]='X';
	for(int i=0;i<=9;i++){
		for(int j=0;j<=9;j++){
			cout<<tab[i][j]<<" ";
		}
		cout<<endl;
	}
	}
}

int main()
{
	int x=5,y=5,n=1;
	char tab[10][10];
	char deplacer;
	remplir_tableau(tab);
	afficher_tableau(tab,x,y,n);
	cin>>deplacer;
	switch(deplacer){
		case 'z':
			x=x-1;
			y=y;
			afficher_tableau(tab,x,y);
		break;
		case 'q':
			x=x;
			y=y-1;
		break;
		case 's':
			x=x+1;
			y=y;
		break;
		case 'd':
			x=x;
			y=y+1;
		break;
		default: cout<<"Vous devez entrer : z q s d !!"<<endl;
	}
	
}
Last edited on
Your program doesn't compile.

As far as logic goes, one problem is that you do
tab[x][y]='X';
But you never reset that back to '-'.

You can do something like this:
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
69
70
71
72
73
#include<iostream>
#include<stdlib.h>
#include<time.h>
#include<iomanip>

using namespace std;

void remplir_tableau(char tab[10][10])
{
	for (int i=0;i<=9;i++)
	{
		for (int j=0;j<=9;j++)
		{
			tab[i][j]='-';
		}
	}
}

void afficher_tableau(char tab[10][10],int x, int y){
	tab[x][y]='X';
	for(int i=0;i<=9;i++){
		for(int j=0;j<=9;j++){
			cout<<tab[i][j]<<" ";
		}
		cout<<endl;
	}
}

int main()
{
	int x=5,y=5;
	char tab[10][10];
	char deplacer;
	remplir_tableau(tab);
	afficher_tableau(tab,x,y);
	
	while (true)
	{
		cin>>deplacer;
		switch(deplacer){
		case 'z':
			tab[x][y] = '-';
    			x=x-1;
    			y=y;
			afficher_tableau(tab,x,y);
			break;
		case 'q':
			tab[x][y] = '-';
    			x=x;
    			y=y-1;
    			afficher_tableau(tab,x,y);
    		break;
    		case 's':
    			tab[x][y] = '-';
    			x=x+1;
    			y=y;
 			afficher_tableau(tab,x,y);
			break;
		case 'd':
			tab[x][y] = '-';
			x=x;
			y=y+1;
			afficher_tableau(tab,x,y);
			break;
    		default:
			cout<<"Vous devez entrer : z q s d !!"<<endl;
			break;
		}
	}
	

	
}
Last edited on
closed account (SiAR92yv)
Thanks a lot @Ganado .
The code is working fine right now ... i am looking for a function or a way to change the position at the same array too ... i mean changing position of 'X' without displaying the array multiple times.
Under normal circumstances, you have to write it multiple times. That's the nature of console output. There could be some OS-specific way to write to specific blocks on the screen, but I do not know of such ways.

What I would do is just clear your screen between each render, please see this article: http://www.cplusplus.com/articles/4z18T05o/
"Clear the screen"

If you're on Windows, see:
http://www.cplusplus.com/articles/4z18T05o/#Windows
Last edited on
closed account (SiAR92yv)
Yes i tried to use before system("CLS") and it clears the windows but it just seems i don t really know where to put it or how to use it.
closed account (SiAR92yv)
and it works fine right now . i have put the system("PAUSE") in the beggining of the function . thanks a lot @Ganado
This might work under windows.

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
69
70
71
72
73
74
75
76
77
78
79
80
#include<iostream>
#include<windows.h>

const int N = 10;

using namespace std;

//======================================

void gotoxy( int x, int y )
{
   static HANDLE h = GetStdHandle( STD_OUTPUT_HANDLE );
   COORD c = { x, y };
   SetConsoleCursorPosition( h, c );
}

//======================================

void fill_table( char tab[N][N], char c )
{
   for ( int i = 0; i < N; i++ )
   {
       for ( int j = 0; j < N; j++ ) tab[i][j] = c;
   }
}

//======================================

void draw_table( char tab[N][N] )
{
   for ( int i = 0; i < N; i++ )
   {
      for ( int j = 0; j < N; j++ )
      {
         gotoxy( i, j );
         cout << tab[i][j];
      }
   }
}

//======================================

void update_table( int xold, int yold, char cdefault, int x, int y, char c )
{
   gotoxy( xold, yold  );   cout << cdefault;
   gotoxy( x   , y     );   cout << c;
   gotoxy( 0   , N + 1 );   cout << "Enter u, d, l, r: ";
}

//======================================

int main()
{
   char cdefault = '-';
   char c        = 'X';
   char tab[N][N];

   fill_table( tab, cdefault );
   draw_table( tab );

   int x = 5, y = 5;
   update_table( x, y, cdefault, x, y, c );

   char direction;
   while ( true )
   {
      int xold = x, yold = y;

      cin >> direction;

      switch( direction )
      {
         case 'u':   if ( y != 0     ) y--;   break;
         case 'd':   if ( y != N - 1 ) y++;   break;
         case 'l':   if ( x != 0     ) x--;   break;
         case 'r':   if ( x != N - 1 ) x++;   break;
      }
      update_table( xold, yold, cdefault, x, y, c );
   }
}

Topic archived. No new replies allowed.