Please help me

studentCJ (1)
Hi

I am doing the RECURSIVELY FINDING PATHS THROUGH A MAZE project but I got a problem here.

I try to read a file to a 2D char array but it did not work

this is what I have



I tried to print maze[20][20] and the whole maze but the output like this

20|20|||
蘾||
+ + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + +
+ + + + ?Press any key to continue . . .

I believe the unknown character 蘾 represent nothing to be stored in the maze[20][20]. can you help me with this? I cannot find a way to store the space from the file in a char array. Also, I don't know why there is a ? mark before Press any key to continue. Thank you for your help! I am using the VS 2013 on the Windows system.

The file :
20 20
+ ++++++++++++++++++
+ + ++ +++
+++ ++++++ ++ ++
+ +++ +++ ++++
+ + ++ ++++++ +
+++ ++ ++ ++ +
+ +++ ++++ ++ ++ +
+ ++++ ++ +++++ +
+ + +++ +++ +
+ ++ + +++ ++++++ ++
+ +++++ +++ ++
+ +++ ++++ ++ ++
+ ++++ +++ +
+++ +++++++++++ +
++ +++++++ +
++ +++ ++++++++ +
++ ++++++ + +
+ +++ ++++++ +++
++++ +++ +++
+++++++++++++ ++++++

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
 #include <cstdlib>
#include <fstream>
#include <iomanip>

#include <iostream>
#include <string>
using namespace std;
int main()
{
int count,i,j,m,l;
char data[1672];
int size[2];
char maze[100][100];

ifstream inputfile;
inputfile.open("maze.dat");
count = 0;
l = 0;
while (l!=2)
{
inputfile >> size[l];
l++;
}
cout << size[0] << "|" << size[1] << "|||"<<endl;




int c=0;
while (!inputfile.eof())
{
for (i = 0; i < size[0]; i++)
{
for (j = 0; j < size[1]; j++)
inputfile >> maze[i][j];
}
}

cout << maze[20][20] << "|||"<<endl;


for (i = 0; i < size[0]; i++)
{
for (j = 0; j < size[1]; j++)
{

cout << maze[i][j] << " ";
}
cout << '\n';
}

system("pause");

return 0;
}
Step 1 is learn how to indent code.
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
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>
using namespace std;

int main()
{
  int count, i, j, m, l;
  char data[1672];
  int size[2];
  char maze[100][100];

  ifstream inputfile;
  inputfile.open("maze.dat");

  count = 0;
  l = 0;
  while (l != 2) {
    inputfile >> size[l];
    l++;
  }
  cout << size[0] << "|" << size[1] << "|||" << endl;

  int c = 0;
  while (!inputfile.eof()) {
    for (i = 0; i < size[0]; i++) {
      for (j = 0; j < size[1]; j++)
        inputfile >> maze[i][j];
    }
  }

  cout << maze[20][20] << "|||" << endl;

  for (i = 0; i < size[0]; i++) {
    for (j = 0; j < size[1]; j++) {
      cout << maze[i][j] << " ";
    }
    cout << '\n';
  }

  system("pause");

  return 0;
}

No one wants to read (or can follow) code which is all stuck at the left hand side.


> inputfile >> maze[i][j];
The big problem here is that >> reading into a char variable reads EVERY character from the file.
This also includes all those newlines between each maze row (and also the newline after the initial 20 20).

You need to find a way to remove all those newlines ('\n') from your data.

To solve the problem that salem c mentions, use getline() to read each line into a string. Then copy the characters of the string into the maze.

I'm a little confused though. The input lines you posted aren't all 20 characters long. What should the maze values be for lines that are short?

cout << maze[20][20] That's almost certainly wrong. Even with a 20x20 maze, this entry in maze won't be initialized. And what if the maze is 25x13? Then maze[20][20] won't mean anything at all.

1
2
3
4
5
  l = 0;
  while (1 != 2) {
    inputfile >> size[1];
    l++;
  }
Never ever ever use l as a variable name. It's too easily mistaken for the digit one. Don't believe me? Two of those el's above are actually one's.

Also, the loop would be better written as a for loop:
1
2
3
for (int i=0; i<2; ++i) {
    inputfile >> size[i];
}

Actually, I wouldn't even use an array for size. I'd use two variables:
1
2
int rows, cols;
input file >> rows >> cols;


> I'm a little confused though. The input lines you posted aren't all 20 characters long.
My guess is the board ate all the consecutive spaces when it got posted.
hi
Fixed my coed but new problem appear.
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
#include <fstream>
#include <iostream>
using namespace std;

int main()
{
	// Variable Declaration
	int i, j;
	int m, n;                           // Holds the size of maze
	char maze[100][100] = { NULL };       // Holds the maze structure from input file
	ifstream inputfile;                   // Variable to read the input file

	// Open the input file and exit if file not found
	inputfile.open("maze.dat");
	if (inputfile.eof())
	{
		cout << "File not found." << endl;
		system("pause");
		return 0;
	}

	// Read and display the size of maze
	inputfile >> m >> n;
	cout << m << "|" << n << "|||" << endl;

	// Read the maze structure from input file and store in maze[][] array
	for (i = 0; i < m; i++)
	{
		for (j = 0; j < n; j++)
		{
			inputfile >> maze[i][j] >> noskipws;   // Read without skipping any character from file
			if (maze[i][j] == ' ')                   // Read next character if new line is encountered
				inputfile >> maze[i][j] >> noskipws;
		}
	}

	// Display the result
	for (i = 0; i < m; i++)
	{
		for (j = 0; j < n; j++)
			cout << maze[i][j] << " ";
		cout << endl;
	}

	// Close the file
	inputfile.close();

	system("pause");
	return 0;
}

The output from vs 2013 is like
20|20|||
+ + + + + + + + + + + + + + + + + + +

+ + + + + + +
+ + + + + +
+ + + + + + +
+ + + + + + +
+ + + +
+ + + + + + + + + + +

+ + + + + + + + + +
+ + + +
+ + + + + + + + +
+ + + + + + + +
+ + + + +
+ + + + + + + + +

+ + + + + + + + + + + + + + +
+ + +
+ + + + + + + +
+ + + + + +
+ + + + + +
+ + + + + + + +
+
+ + + + + + + + + + + + + + +

+ + + + + + + + + +
+ + +
+ + + + + + + + + + +
+ + + + + +
+ + + +
+ + + + + + + + +
+ + + +
+ + + + + + + + + +

+ + + + + + + + + + + + + + + + + + +




Press any key to continue . . .
The output does not look like a maze...

Please help thank you very much
Last edited on
The maze.dat
+ ++++++++++++++++++
+ + ++ +++
+++ ++++++ ++ ++
+ +++ +++ ++++
+ + ++ ++++++ +
+++ ++ ++ ++ +
+ +++ ++++ ++ ++ +
+ ++++ ++ +++++ +
+ + +++ +++ +
+ ++ + +++ ++++++ ++
+ +++++ +++ ++
+ +++ ++++ ++ ++
+ ++++ +++ +
+++ +++++++++++ +
++ +++++++ +
++ +++ ++++++++ +
++ ++++++ + +
+ +++ ++++++ +++
++++ +++ +++
+++++++++++++ ++++++
I don't know why it does not look good here or in my PC.
Seem like some space disappear
Last edited on
You need to put it between [output] [/output] tags to retain spacing.

+ ++++++++++++++++++
+          +  ++ +++
+++ ++++++   ++   ++
+ +++    +++    ++++
+   + ++ ++++++    +
+++   ++     ++ ++ +
+   +++ ++++ ++ ++ +
+ ++++  ++   +++++ +
+    + +++ +++     +
+ ++ + +++ ++++++ ++
+  +++++   +++    ++
+ +++    ++++  ++ ++
+   ++++      +++  +
+++    +++++++++++ +
++  +++++++        +
++ +++    ++++++++ +
++ ++++++        + +
+   +++   ++++++ +++
++++     +++     +++
+++++++++++++ ++++++


BTW, your thread title is terrible. "Recursive maze solving" would be better than "please help me".
Last edited on
Like I said, use getline() to read the maze data.
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
#include <fstream>
#include <iostream>
#include <cstring>

using namespace std;

int
main()
{
    // Variable Declaration
    int i, j;
    int m, n;			// Holds the size of maze
    char maze[100][100] = { 0 };		 // Holds the maze structure from input file
    ifstream inputfile;		// Variable to read the input file

    // Open the input file and exit if file not found
    inputfile.open("maze.dat");
    if (inputfile.eof()) {
	cout << "File not found." << endl;
	system("pause");
	return 0;
    }
    // Read and display the size of maze
    inputfile >> m >> n;
    cout << m << "|" << n << "|||" << endl;
    inputfile.ignore(100000, '\n'); // skip past end of line
    // Read the maze structure from input file and store in maze[][] array

    for (i = 0; i < m; i++) {
	string line;
	getline(inputfile, line);
	if (line.size() != m) {
	    cerr << "line " << i+1 << " is "
		 << line.size() << " characters instead of " << m << '\n';
	} else {
	    strcpy(maze[i], line.c_str());
	}
    }

    // Display the result
    for (i = 0; i < m; i++) {
	for (j = 0; j < n; j++)
	    cout << maze[i][j] << " ";
	cout << endl;
    }

    // Close the file
    inputfile.close();

    system("pause");
    return 0;
}


maze.dat:
20 20
+ ++++++++++++++++++
+          +  ++ +++
+++ ++++++   ++   ++
+ +++    +++    ++++
+   + ++ ++++++    +
+++   ++     ++ ++ +
+   +++ ++++ ++ ++ +
+ ++++  ++   +++++ +
+    + +++ +++     +
+ ++ + +++ ++++++ ++
+  +++++   +++    ++
+ +++    ++++  ++ ++
+   ++++      +++  +
+++    +++++++++++ +
++  +++++++        +
++ +++    ++++++++ +
++ ++++++        + +
+   +++   ++++++ +++
++++     +++     +++
+++++++++++++ ++++++


output of program:
20|20|||
+   + + + + + + + + + + + + + + + + + +
+                     +     + +   + + +
+ + +   + + + + + +       + +       + +
+   + + +         + + +         + + + +
+       +   + +   + + + + + +         +
+ + +       + +           + +   + +   +
+       + + +   + + + +   + +   + +   +
+   + + + +     + +       + + + + +   +
+         +   + + +   + + +           +
+   + +   +   + + +   + + + + + +   + +
+     + + + + +       + + +         + +
+   + + +         + + + +     + +   + +
+       + + + +             + + +     +
+ + +         + + + + + + + + + + +   +
+ +     + + + + + + +                 +
+ +   + + +         + + + + + + + +   +
+ +   + + + + + +                 +   +
+       + + +       + + + + + +   + + +
+ + + +           + + +           + + +
+ + + + + + + + + + + + +   + + + + + +

Registered users can post here. Sign in or register to post.