# I can't seem to find the problem

This topic is 4853 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

It's probly obvious but I don't see what it could be. Ignore the comments.
void Map::GetCharsXY(char SearchFor)
{
// open map file
ifstream fin;
fin.open(MapFile);

// Define variables
char TheLength[1000];
char CharMap[1000];
int y = 1;
int i = 0;
int TheWidth;
int MaxX;// The height
int MinimumForLoop;
int MaximumForLoop;

// Get the first line, which'll be used to find the length of(width of the map)
fin >> TheLength;

// Reset position to the beggening
fin.seekg(ios_base::beg);

TheWidth = strlen(TheLength); // Get the length of TheLength to find the width of the map

// Go character by character, when the 'TheWidth' is met y is set back to 1,
// untill the end of the file getting the verticle line the searched character is on,
// and i is how many characters in total it is from the first
while(!fin.eof())
{
CharMap = fin.get();
y++;
i++;

if(y == TheWidth)
{
y = 1;
}

if(CharMap == SearchFor)
{
CharY = y;
break;
}

}

// Reset position to the beggening
fin.seekg(ios_base::beg);

// Used to find how many lines the map is(the height) ='s 1 since it starts at line 1
MaxX = 1;
// Loop through untill the end of the file - storing how many lines in MaxX
while(!fin.eof())
{
fin >> TheLength;
MaxX++;
}

// Used for calculating the horizontal line read down further
MinimumForLoop = 1 - 1;
MaximumForLoop = TheWidth + 1;

// Close the map file
fin.close();

// Since i started at 0, to get the correct number of characters from the first
// it is you have to add 1 to it
i++;

// Loop through however many times as the height is big if the map was:
/*
ooooo
toooo
~~~~~

to get the horizontal line we'll use this:

if we searched for 't' -
the width is 5.
the height is 3.
t is 6 characters away from the first.

MinimumForLoop starts at 0
MaximumForLoop starts at 6(width + 1)

first loop:
if 6(from first) is greater than MinimumForLoop(0) and it's smaller than MaximumForLoop(6),
then it's on the first line.
6 is not <> 0 or 6 so the next loop
second loop:
MinimumForLoop will now be at 0 + TheWidth making it = 5.
MaximumForLoop will now be at 6 + TheWidth making it = 11.
if 6(from first) is greater than MinimumForLoop(5) and it's smaller than MaximumForLoop(11),
then it's on the second line.
true:
CharX  = Loop//CharX = is the Chars location, Loop is how many times the loop went through which is 2
and break, stoping the loop.
Now CharY = y // y is the verticle line its on
and CharX = Loop // The verticle line

problem is it returns 763312, which might be different for the character searched..but its odd either way

it's ment to search for the 'x' or w/e it'll be - which is used for the character's(litteral, not 'char') location so there'll be just 1.

*/
for(int Loop = 1; Loop >= MaxX; Loop++){

if(i > MinimumForLoop && i < MaximumForLoop )
{
CharX = Loop;
break;
}
MinimumForLoop = MinimumForLoop + TheWidth;
MaximumForLoop = MaximumForLoop + TheWidth;

}
}


I'm making a rpg with a friend - which is a crappy console one but o well, using the all famous maps of o's ^'s ~'s etc. Take into consideration this map: ooooo oooox ooooo x's 'x' is 2 and it's 'y' is 2, second line - second down. The code supposivly 'takes the coordinates or w/e and places them into 'CharX' and 'CharY' which is defined in the class Map. So far everything goes well except - the coordinates are not correct. They are around 763312, 7633012, etc(if you can find a pattern even :/). There is no relationship with these numbers that are given to the correct location. Out of desperation I posted this to see if anyone could spot the flaw or w/e the mishap is.

##### Share on other sites
You define char CharMap[1000] which is an array of 1000 characters which is not initialized, thus filled with random values.

Now you read a map character and store in the array above.
BUT, you increase 'i' and then you do

if(CharMap == SearchFor)

i is now indexing to the next array cell which is not yet initialized !

##### Share on other sites
I didn't need to make it an array - so I took it off, and it made it almost there - the CharX is still messed, and I think CharY is storing the X :/...but it's closer, thank you. I'll figure it out soon enough.

##### Share on other sites
Yes the CharY is actually the x coordinate so swap between the two variables

##### Share on other sites
I'm sorry but that's some really ugly and error-prone code there. How about something like:
#include <fstream>#include <iostream>#include <stdexcept>#include <string>#include <utility>#include <vector>class Map{	public:		// provide a default constructor		Map();		// construct a map from a given filename		Map(std::string filename);		// find the location of the given character in a map		std::pair< unsigned int, unsigned int > find(char target);	private:		std::vector< char > map_;		unsigned int width_;		unsigned int height_;};// default constructor implementation// no initialisation required since width_ and height_ are only used if map_ is not emptyMap::Map(){}// constructor implementation to construct a map from a given filename// initialise width_ and height_ to zeroMap::Map(std::string filename)	:	width_(0),	height_(0){	// open the file	std::ifstream reader(filename.c_str());	std::string line;	// read a line from the file	while (std::getline(reader, line))	{		// if we know the width of the map then check the new line has the correct width		if (width_ != 0)		{			if (line.size() != width_)			{				throw std::range_error("map is not square");			}			++height_;		}		// this is the first (non-empty) line of the map so get the width		else		{			if (!line.empty())			{				width_ = line.size();				++height_;			}		}		// add the line to the linear representation of the map (rows stored one after another)		map_.insert(map_.end(), line.begin(), line.end());	}}std::pair< unsigned int, unsigned int > Map::find(char target){	// find the location of the first occurance of target in the linear representation of the map	std::vector< char >::const_iterator location = std::find(map_.begin(), map_.end(), target);	// target was not found, throw an error	if (location == map_.end())	{		throw std::runtime_error("character not found in map");	}	// calculate 2d index of character and return it	unsigned int locationIndex = location - map_.begin();	return std::pair< unsigned int, unsigned int >(locationIndex % width_, locationIndex / width_);}int main(){	Map map("map.txt");	std::pair< unsigned int, unsigned int > location = map.find('x');	std::cout << "'x' is at: " << location.first << ", " << location.second << '\n';}

Enigma

1. 1
Rutin
40
2. 2
3. 3
4. 4
5. 5

• 12
• 17
• 12
• 14
• 9
• ### Forum Statistics

• Total Topics
633358
• Total Posts
3011511
• ### Who's Online (See full list)

There are no registered users currently online

×