I'm working on a map editor and the tiles show up i a file as

1, 0, 0, 1,
1, 0, 1, 1

so here's a 4x2 map. I'm wondering how can I parse out the integers from the commas and assign them to a multidimensional array so that \i can move onto drawing the tiles to the screen...
Thanks for any help!

Read an int, then ignore a single character. Optionally peek to see if the character is a comma for more robust error checking:

for (int i = 0; i < 2; i++) {
    for (int j = 0; j < 4; j++) {
        if (!(in>> a[i][j]))
            break;
        in.ignore();
    }
}

whatever happened to strtok() lol

whatever happened to strtok() lol

Why complicate matters when you don't have to?


lol :icon_rolleyes:

I would have thought that using this input:

1,

with this syntax:

fin >> a[j];

where a[j] is an int would result in fin going into an error state and that the stream would need to be reset/clearred before the char, the comma, that caused the error could be ignored. Guess I'll have to check it out when I get a compiler available.

I would have expected the posted syntax to work if the input had been:

1 , 0 , etc.

I would have used getline() with comma as the delimiter and then convert the input string into the desired numerical value using a stringstream; but, as I'm fond of saying, live and learn.

I would have thought that using this input:

1,

with this syntax:

fin >> a[j];

where a[j] is an int would result in fin going into an error state

Nope, input simply stops at the first non-integer character. The stream state isn't affected.

I would have expected the posted syntax to work if the input had been:

1 , 0 , etc.

Actually, that would not have worked without an extra step of trimming leading whitespace before extracting the comma. Leading whitespace after the comma is fine because operator>> will skip it automatically when reading an integer. Only the bogus integer character (a comma) needs to be removed.

I would have used getline() with comma as the delimiter and then convert the input string into the desired numerical value using a stringstream

Once again, why make it harder than it needs to be? :)

Narue, thank you! Your solution helped greatly and this works. I'm just wondering, is this the neat and proper way to do what you're explaining?

const int TILE_X = 4;
const int TILE_Y = 4;

int map_tiles[TILE_X][TILE_Y];
	
	//storing map data in map_tiles
	ifstream reader("map.txt");
	if (!reader)
		cerr << "Error opening file";
	else
	{
		for (int i = 0; i < TILE_Y; i++)
		{
			for (int j = 0; j < TILE_X; j ++)
			{
				reader >> map_tiles[i][j];
				reader.ignore();
			}
		}
	}
	
	//displaying map
	//when n = amount of tiles on x axis, create a new line for the next set 
	for (int i = 0; i < TILE_Y; i++)
	{
		for (int j = 0, n = 0; j < TILE_X; j++)
		{
			cout << map_tiles[i][j];
			n++;
			if (n == TILE_X)
				cout << "\n";
		}
	}

That's fine. For extra l33t points, you can also merge the input and output loops, or even get rid of the array altogether if that's the only place you use it.

void load_map(const char* filename)
{
	ifstream reader(filename);
	if (!reader)
		cerr << "Error opening file";
	else
	{
		for (int i = 0; i < TILE_Y; i++)
		{
			for (int j = 0, n = 0; j < TILE_X; j ++)
			{
				reader >> map_tiles[i][j];
				reader.ignore();
				cout << map_tiles[i][j];
				n++;
				if (n == TILE_X)
				cout << "\n";
			}
		}
	}
}

There's a merged function that can upload any txt file map... Hmm, how would I do it without the array? And I'm wondering about the necessity of it now, won't it be important in a map editor, or have I no idea?

Well, if you need the array for other purposes then it has to be there. But if all you're doing is reading from a file and displaying the contents, you can simply pull one record at a time and not bother storing past records:

for (int i = 0; i < TILE_Y; i++) {
    for (int j = 0; j < TILE_X; j++) {
        int tile;

        reader>> tile;
        reader.ignore();

        cout<< tile;
    }

    cout<<'\n';
}

great, i changed it to that as I don't believe I'll need the array too much.. Now I just need to include Allegro and change the cout to blit and blit the appropriate tiles according to the tile number, thanks so much or your help Narue!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.