As topic asks, how do I transfer the varaibles located in my 1d array to a 2d array? Currently, my code does not seem to transfer the codes (either that or my cout is wrong). I am sure that my 1d array has variables and stuff, but it is not passing it into the 2d array.
My buffer variables are not here, but rest assured, they have values in them.
So I rectified it, as well as put new intergers for cols and rows when using cout.
I also don't understand your second note. I did set y=0 right before I set x. I don't see any 0<y conditions anywhere either.
However, I get a bunch of gibberish when I try to cout.
My buffer's variable is actually taken from an external text file using get(). I have a suspicion that it could be because it also takes in the newline character, but I'm not sure if it'll mess with my 2d array like so.
{
int cols =61;
int rows =20;
char buffer[1220];
ifstream Maps;
Maps.open ("Map01.txt"); //open file
if(!Maps){ //check if file is open
cout<< "error";
}
Maps.get(buffer, sizeof(buffer), '!'); //Retrieve character value and give it tobuffer
char ** maze = newchar * [rows]; //create array using pointers and double pointer
for (int r = 0; r < rows; r ++) //ditto
{
//maze[r] = new char [cols]; //same
}
int y=0;
int x=0;
for (int i=0;i<1220;i++) //transfer buffer values to maze
{
if (x != 61)
{
maze[y][x] = buffer[i];
x++;
}
else
{
y++;
x=0;
}
}
for (int i=0; i<60;i++)
{
for (int o = 0; o <20; o++)
{
cout <<maze [i][o];
}
}
cout<<endl;
//cout<<buffer<<endl;
system ("pause");
}
On the second note:
1. Lets be at i==60, y==0, x=60. x!=61, so maze(0,60) = buffer(60) and both i and x increment to 61.
2. Now x==61, y increments to 1, x resets to 0, and i increments to 62.
3. i==62, y==1, x==0, so x!=61 and maze(1,0) = buffer(62).
Now, did you notice how on step 2 the buffer(61) was not stored anywhere?
Why do you use magical numbers on your lines 38 and 40? In line 17 you had rows elements in maze. In line 42 maze seems to have 60 elements. Does it? No.