How can I create the map size,

#include <iostream>
#include <fstream>
#include<string>
using std::ifstream;
bool msize(int Height,int Width){

//add code

return true;
}
bool draw_map(char* Filename,int Width,int Height){

	if(Width<1||Height<1)
		return false;
	msize(Height,Width);
	ifstream file;
	file.open(Filename);

	if(file.is_open()){
			
		while(!file.eof()){	

			for(int x=0;x<Height;x++){

				for(int y=0;y<Width;y++){

					if(c!=']'){
						file >> map[x][y];
						file.get(c);

					}else 
						map[x][y] = -1;
				}
			}

		 }
		file.close();
	}
	return true;
}

Recommended Answers

All 9 Replies

I didn't see this post before responding in your other post.

I'd recommend using a std::vector<std::vector<int> > . You can then resize on the fly.

(not tested... just a guideline)

void msize(int Height,int Width,  std::vector<std::vector<int> > map)
{

map.resize(width); // you'll have to pick if the outer vector or the inner vectors are the rows or cols
for(unsigned int i = 0; i < map.size(); i++)
  {
  map[i].resize(height);
  }

}

David

What can I write in std::vector<std::vector<int> > map?

You get to do

yourMap[i][j] = an int;

(Just like a 2d array)

Can you explain to me? Where is my mistake?

msize(3,3,int yourMap[i][j]);
std::vector<std::vector<int> > yourGrid;
msize(3,3,yourGrid);

How can I write this map in my vector

bool msize(int Height,int Width, std::vector<std::vector<int> > map)
   
      {
   
    if(Height<1||Width<1)return false;

      map.resize(Width);
   
      for(unsigned int i = 0; i < map.size(); i++)
   
      {
   
      map[i].resize(Height);
   
      }
   return true;
       

      }
bool draw_map(char* Filename,int Width,int Height){
	
	if(Width<1||Height<1)
		return false;
	
	ifstream file;
	file.open(Filename);
	if(file.is_open()){

		while(!file.eof()){	

			for(int x=0;x<Height;x++){

				for(int y=0;y<Width;y++){

					if(c!=']'){
						file >> Map[x][y];
						file.get(c);

					}else 
						Map[x][y] = -1;
				}
			}

		 }
		file.close();
	
	}
	return true;
}

Looks good. You just have to get the Map variable into the draw_map function.

Thanks

alex,

Us telling you exactly where to put each character is not helpful for anyone involved. I'd suggest finding a basic c++ tutorial dealing with functions, arguments, and parameters. Compiling a few basic things and playing with them will help you get a handle on things. Then we can return to your vector question if you still have a problem.

David

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.