Hello! i need some help! im trying to figure out how to take a .txt file an put it into a char array so i can use it for my game. I dont understand what im doing wrong. can someone please help me.
if (myfile.is_open())
{
for (int a = 0; a != 16 && getline (myfile,line); a++ )
{
for(int b = 0; b != 49; b++)
{
Map[a][b] = line[b];
}
cout << '\n';
}
myfile.close();
}
You were counting a from 0 for each line. That means you were writing the same thing to each line. You only want to increment a when you get a new line.
int row = 0;
while(std::getline(myfile, line))
{
//iterate over string and assign or use a stringstream
std::stringstream ss(line);
int column = 0;
while(ss >> Map[row][column++]);
++row;
}
*edit forgot the column variable
PS you shouldn't use magic numbers like 16 and 49. You should assign them to a constant variable like rows and columns. Then you can use for loops and what ever else needs the rows/columns very easy and you can change the rows/columns without having to modify all the 16s/49s.
i know. i only used numbers here to try an figure it out. once i get it figured out completely im going to use it in my game. its an ASCII scroller in the consol. next step after this is scrolling