inputFile and 2d array issues

So the premise of my code is to read in a 2d array from a .txt. The array is a game board but the first two lines are what determine the size of the board. After i read it in i want it to find wherever te character "U" is, and then display the array but only showing U and whats around it. Problem is i cant get the array to print the right size and the code for displaying the U is not working either.

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
ifstream inputFile;
	int boardSizeRow;
	int boardSizeCol;
	inputFile.open("C:\\Users\\Michael\\Desktop\\fileboard2.txt");
	inputFile >> boardSizeRow;
	inputFile >> boardSizeCol;
	inputFile.get();

	
	char gameBoard[20][20];
	for (int row = 0; row < boardSizeRow; row++)
	{
		for (int col = 0; col < boardSizeCol; col++)
		{
			gameBoard[row][col] = inputFile.get();
		}
	}


	for (int row = 0; row < boardSizeRow; row++) //////////////TO TEST PRINT
	{
		for (int col = 0; col < boardSizeCol; col++)
		{
			cout << gameBoard[row][col];
		}
	}

	cout << endl;
	cout << endl;

	const int ROWS = 20; 
	const int COLS = 20;
	bool toPrint[ROWS][COLS] = {false}; 
	for (int i = 0; i < ROWS; i++ )
	{
		for (int j = 0; j < COLS; j++)
		{
		   if (gameBoard[i][j] == 'U')
		   {
				//set parameters around:
				toPrint[i][j] = true; 
				toPrint[i][j-1] = true; //West
				toPrint[i][j+1] = true; //East
				toPrint[i-1][j] = true;  //North
				toPrint[i+1][j] = true; //South
		   }
	   }
	}
	for (int i = 0; i < ROWS; i++ )
	{
		for (int j = 0; j < COLS; j++)
		{
		   if (toPrint[i][j])
		   {            
			   cout << gameBoard[i][j] ;
		   }
		   else
		   {
			   cout <<"0";
		   }
		}
		cout <<endl;
	 }
	cout << endl; 

	return 0;
}




FILE:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
20
20
WWWWWWWWWWWWWWWWWWWW
  W GO  W          W
W WW      w    S   W
W H W   GW  w      W
WPW  WW          G W
 WK       W        W
W W W  W    w   w  W
  WK WU            W
    SW      w  w   W
           W       W
    w    W       G W
  G    W       w   W
D   wwwww          W
         K   w  D  W
w w   W w   w      W
    ww  w    WWWWWWW
  G        w       W
    ww  w S    w   W
   WWW      G      W
WWWWWWWWWWWWWWWWWWWW
Last edited on
get returns unformatted data and you are not handling new line characters.
try using some other functions like getline().

1
2
3
4
5
6
	for (int row = 0; row < boardSizeRow; row++)
	{
			std::string line;
			getline(inputFile, line);
			strcpy(gameBoard[row], line.c_str());
	}


EDIT: also, reading the array size from text file and then doing this
char gameBoard[20][20];
makes no sense.
Last edited on
Yea someone else told me that, well i have to have it where it reads in the size, so how do i initiate the array with the only way o knowing the diminisions by reading in the LxW?
use pointers.. dynamic allocation using new/malloc.
sorry shouldve mentioned before that we cant use pointers
Then use std::vector instead of C-style arrays. They can be dynamically sized.
Topic archived. No new replies allowed.