Public Group

#### Archived

This topic is now archived and is closed to further replies.

# getting bugs trying to make dynamic arrays

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

## Recommended Posts

bool LoadMap()
{

Randomize_Map_Color();

string filename[4]; //this holds the string of which map to load

filename[0] = "Map1.txt";
filename[1] = "Map2.txt";
filename[2] = "Map3.txt";
filename[3] = "Map4.txt";

// Create a "ifstream" (way to read from a file) for the map text file

ifstream file_in(filename[gamedata.current_level - 1].c_str());
//loads the map for whatever level it is which was determined the line above this

// Error Check -- If we couldn''t open the file, we can''t load the map

if(!file_in)
return false;

// First line of "map file" contains two integers.

// First int is the width of the map

// Second int is the height of the map

file_in >> gamedata.width; // Read in the width of the map

file_in >> gamedata.height; // Read in the height of the map

file_in >> gamedata.number_of_monsters;  //read in how many "monsters" are on this map

//	char *monsters = new char[gamedata.number_of_monsters];

// Now we can create a CHAR_INFO array that is big enough to hold the map

gamedata.screenBuff = new CHAR_INFO[gamedata.width * gamedata.height];

gamedata.monsters = new char[gamedata.number_of_monsters];

//make a new set of monsters depending on how many are specified in the map

// Error check -- Make sure we could allocate the memory

if(gamedata.screenBuff == NULL || gamedata.monsters == NULL)
{
file_in.close(); // Close the file we opened

return false;
}

//for (int i = 0 ; i < gamedata.number_of_monsters ; i++)

//fill up each monster with color and an acii char

//{

//monster[i].dispChar = (char)234;

//monster[i].color = gamedata.current_level_floor | FOREGROUND_GREEN | FOREGROUND_INTENSITY;

//if (i > 0)

//	monster[i].color = gamedata.current_level_floor | FOREGROUND_RED;

//else if (i > 1)

//	monster[i].color = gamedata.current_level_floor | FOREGROUND_BLUE;

//}

//i = 0;

// These will be used to index into our screenBuff

// *Remember* even though "screenBuff" is a one dimensional array, we''re going to

// treat it as a two dimensional array

int x = 0;
int y = 0;
int i = 0; // a counter used in the Monster part of the map placement

char temp; // This will be a "temp" char -- This will hold the current character

// we''ve read in from the file

while(y != gamedata.height) // Keep going until we reach the end of the "map"

{
file_in >> temp;

switch(temp)
{
case ''0'': // We''ve read in an "open space" in our maze

// Fill the current location in the screen buffer with a space character ('' '')

gamedata.screenBuff[x + y * gamedata.width].Char.AsciiChar = '' '';
gamedata.screenBuff[x + y * gamedata.width].Attributes = gamedata.current_level_floor;

break;

case ''#'':  // We''ve read in a "wall" in our maze

// Fill the current location in the screen buffer with the character

// defined by WALL

gamedata.screenBuff[x + y * gamedata.width].Char.AsciiChar = WALL;
gamedata.screenBuff[x + y * gamedata.width].Attributes = gamedata.current_level_wall_BG | gamedata.current_level_wall_FG;

break;

case ''E'':
gamedata.screenBuff[x + y * gamedata.width].Char.AsciiChar = ''E'';
gamedata.screenBuff[x + y * gamedata.width].Attributes = gamedata.current_level_wall_BG | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
gamedata.xEndPos = x;
gamedata.yEndPos = y;
break;

case ''T'':
gamedata.screenBuff[x + y * gamedata.width].Char.AsciiChar = ''T'';
gamedata.screenBuff[x + y * gamedata.width].Attributes = gamedata.current_level_wall_BG | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
gamedata.xTreasPos = x;
gamedata.yTreasPos = y;
break;

case ''M'':

monster[i].dispChar = (char)234;
monster[i].color = gamedata.current_level_floor | FOREGROUND_GREEN | FOREGROUND_INTENSITY;

monster[i].xPos = x; //give this monster a unique identity (monster[i] where i is unique)

monster[i].yPos = y;

gamedata.screenBuff[x + y * gamedata.width].Char.AsciiChar = monster[i].dispChar;
gamedata.screenBuff[x + y * gamedata.width].Attributes = monster[i].color; //;gamedata.current_level_wall_BG | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | BACKGROUND_RED;

i++; //increment to the next monster

break;
//gamedata.

default: // If we read any other character (char), there''s an error in our

// map file -- So close it and return

file_in.close();
return false;
}

// Increment ''x'' every time we write a character until ''x'' equals the

// width of map, then start ''x'' back at zero

if(++x == gamedata.width)
{
x = 0;
y++; // We need to increment y so we effectively "go down to the next

// line" in our screen buffer

}

} // end of while(y != gamedata.height)

// Okay if we get here the map has been loaded successfully --

// Now all we have to do is close the file we opened and then return true

file_in.close();
return true; // Success :)

} // end of bool LoadMap()


anyway,

##### Share on other sites
You need to do a big more debugging. Put a brakpoint in the monster case and see what happens for the 7th monster - has the array been allocated high enough? And what is the error message you get? Try also to use better variables like numMonsters and currentMonster and then put checks in like assert(currentMonster
Also you maybe better off using a vector as it can dynamically grow as you add monsters.

##### Share on other sites
1) You seem to have a struct that represents your monsters, but you''re allocating an array of single characters to store them. Unless it''s deliberate that
2) "gamedata.monsters" doesn''t match "monster". Where is your "monster" array defined/declared? Is it possible that *that* one isn''t big enough? I don''t see that anything is being done with "gamedata.monsters" after the allocation.

3) Don''t check for NULL after new. It can''t give you that as a result. If the allocation failed, it will throw std::bad_alloc - you need a try/catch block to handle the exception.

1. 1
2. 2
frob
16
3. 3
4. 4
5. 5

• 20
• 13
• 14
• 76
• 22
• ### Forum Statistics

• Total Topics
632139
• Total Posts
3004369

×