Writing a text file into a 2 dimensional array(help!)

Ive been creating a little project that lets the user make a path on a graph using a two dimensional array written on a file(intended for use in a later project, for now it is just a text file with 20x20 area filled with '|'). Problem is that im having trouble actually writing the file into the program. Please help! i dont know what im doing wrong.

Project:
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
#include <iostream>
#include <cstdlib>
#include <string>
#include <fstream>
void createmap(char map[][20], const int n, const int x);
int main()
{
    const int nrows = 20;
    const int ncols = 20;
    int xpos = 5;
    int ypos = 5;
    std::string x;
    char map[nrows][ncols];
    createmap(map, nrows, ncols);
    // this array was used for the original map, im trying now to create  a map in a text file, so ignore this!

        map[5][5] = 'x'; // starting position
        while(std::cin){
            std::cout << "Press w for up! becuase its just a test dummy\n";
            std::cin >> x;

            if(x == "w"){
            ypos -=1;
            map[ypos][xpos] = 'x';
            }
            if(x == "s"){
            ypos +=1;
            map[ypos][xpos] = 'x';
            }
            if(x == "a"){
            xpos -=1;
            map[ypos][xpos] = 'x';
            }
            if(x == "d"){
            xpos +=1;
            map[ypos][xpos] = 'x';
            }

            system("CLS");
            for( int nrow = 0; nrow < nrows; nrow++){
            for(int ncol = 0; ncol < ncols;ncol++)
                std::cout << map[nrow][ncol] << " ";
            std::cout << std::endl;
            }
        }
}

void createmap(char map[][20], const int n,  const int x){
   using namespace std;
    ofstream MapStruct("Map.txt");

    for( int nrow = 0; nrow < n; nrow++){
            for(int ncol = 0; ncol < x;ncol++){
                MapStruct << map[nrow][ncol] << " ";
            }
            cout << endl;
        }

        MapStruct.close();
}


what happens when i run the code:
http://i1228.photobucket.com/albums/ee449/muppetman1/WeirdChars.png

what it should print:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
| | | | | | | | | | | | | | | | | | | |
| | | | | | | | | | | | | | | | | | | |
| | | | | | | | | | | | | | | | | | | |
| | | | | | | | | | | | | | | | | | | |
| | | | | | | | | | | | | | | | | | | |
| | | | | | | | | | | | | | | | | | | |
| | | | | | | | | | | | | | | | | | | |
| | | | | | | | | | | | | | | | | | | |
| | | | | | | | | | | | | | | | | | | |
| | | | | | | | | | | | | | | | | | | |
| | | | | | | | | | | | | | | | | | | |
| | | | | | | | | | | | | | | | | | | |
| | | | | | | | | | | | | | | | | | | |
| | | | | | | | | | | | | | | | | | | |
| | | | | | | | | | | | | | | | | | | |
| | | | | | | | | | | | | | | | | | | |
| | | | | | | | | | | | | | | | | | | |
| | | | | | | | | | | | | | | | | | | |
| | | | | | | | | | | | | | | | | | | |
| | | | | | | | | | | | | | | | | | | |


Please help! i am so confused
Last edited on
closed account (o1vk4iN6)
You want to print the number for bytes? You would need to typecast to int or short instead of "char" (seems unsigned char is interpreted as text as well, sorry my mistake), since char is interpreted as human readable acsii text.

Edit:

You need to initialize your array, right now it's filled with whatever was on the stack.

1
2
3
for(int i = 0; i < nrows; i++)
     for(j = 0; j < ncols; j++)
          map[i][j] = '|';
Last edited on
Topic archived. No new replies allowed.