Reputation: 177
I am a beginner at handling characters - This is likely a rookie mistake. I have a maze that looks like
###################
# #
# * * * * *\* * * #
# #
# * * * * * * * * #
# #
###################
This maze forms a grid with the top left being at the coordinate (0,0). I am trying to store each element of the maze into an array for processing. My current implementation looks like
// Print the world without the following spam and save it to an array
// Create array for storing the world and initialise
char world[width][height];
int i=0;
while(getline(infile, line)){
while(getline(infile, line)){
int y=0;
while(y<width){
world[i][y]=line.at(y);
cout << i << "," << y << "," << world[i][y];
y++;
}
i++;
if(i==height){break;}
}
cout << "backslash?:" << world[2][11] << endl;
The output to the terminal is as follows.
0,0,#0,1,#0,2,#0,3,#0,4,#0,5,#0,6,#0,7,#0,8,#0,9,#0,10,#0,11,#0,12,#0,13,#0,14,#0,15,#0,16,#0,17,#0,18,#
1,0,#1,1, 1,2, 1,3, 1,4, 1,5, 1,6, 1,7, 1,8, 1,9, 1,10, 1,11, 1,12, 1,13, 1,14, 1,15, 1,16, 1,17, 1,18,#
2,0,#2,1, 2,2,*2,3, 2,4,*2,5, 2,6,*2,7, 2,8,*2,9, 2,10,*2,11,\2,12,*2,13, 2,14,*2,15, 2,16,*2,17, 2,18,#
3,0,#3,1, 3,2, 3,3, 3,4, 3,5, 3,6, 3,7, 3,8, 3,9, 3,10, 3,11, 3,12, 3,13, 3,14, 3,15, 3,16, 3,17, 3,18,#
4,0,#4,1, 4,2,*4,3, 4,4,*4,5, 4,6,*4,7, 4,8,*4,9, 4,10,*4,11, 4,12,*4,13, 4,14,*4,15, 4,16,*4,17, 4,18,#
5,0,#5,1, 5,2, 5,3, 5,4, 5,5, 5,6, 5,7, 5,8, 5,9, 5,10, 5,11, 5,12, 5,13, 5,14, 5,15, 5,16, 5,17, 5,18,#
6,0,#6,1,#6,2,#6,3,#6,4,#6,5,#6,6,#6,7,#6,8,#6,9,#6,10,#6,11,#6,12,#6,13,# 6,14,#6,15,#6,16,#6,17,#6,18,#
backslash?:
Why does the final cout not show a backslash?
Upvotes: 0
Views: 582
Reputation: 8514
You have height and width transposed in this declaration:
char world[width][height];
So the shape of your array does not match your input. This means that you are writing outside the bounds of your array (which is Undefined Behavior) and probably overwriting some data.
Upvotes: 1