While Loop is Crashing Program

I am making a program to generate random dungeons. So far I have it where the program can fill the map with solid earth and then carve out a room at a random position on the map as the starting point.

This function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void buildDung(int low, int range)
    {
        /// Finds a suitable area to build a feature ///
        int xCoord = randGen(2,67);
        int yCoord = randGen(2,42);
        while(map[xCoord][yCoord] != " ")
        {
            xCoord = randGen(2,67);
            yCoord = randGen(2,42);
        }
        choose:

        goto choose;
    }

Is giving me a good amount of trouble. This function is supposed to the search spots on the map until it finds a spot where there is no earth. It seems like it would work, but it keeps crashing my program... I have no clue as to why. Any help would be great!
While I can't tell what randGen() does and what map[][] really is, you can't expect control to ever get past line 13. This:

1
2
label:
goto label;

is the same as saying:

while(true) { };

It's just a never ending loop, though I don't see how it could crash the program at all.

Incidentally, don't use the goto statement. There's always a better, structured way to deal with program flow.
Last edited on
I took the goto loop out and it still crashes.

randGen() generates a random number.
map[][] is a multidimensional array that holds the values for each square on the map.
Okay! I found out what is making it crash, but I don't know why.

************************
************************
************************
************************
************************
************************
************************
***************____*****
***************____*****
************************
************************
************************

Suppose that this is the dungeon that my program made. The earth is made of * and the room is just spaces. If I have the while loop search for what character makes up the room (whether is be spaces, &, etc) it crashes. If the while loop search for something on the walls it won't. I don't understand.
Last edited on
randGen() generates a random number.

That I could tell, but you'd need to post the function for us to see what's going on. Is it guaranteed to return only values in the range of map[][]? Is map[][] an array of strings? Can you post its declaration?
string map[x][y];

1
2
3
4
5
int randGen(int low, int range)
    {
        /// Generates random numbers in the range that is specified ///
        return low+int(range*rand()/(RAND_MAX + 1.0));
    }


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void firstRoom()
    {
        /// Carves out the first room of the dungeon ///
        int down = randGen(10,21);
        int left = randGen(10,21);
        int width = randGen(3,6);
        int length = randGen(3,6);
        int a = x-down;
        int aa = a+length;
        int b = y-left;
        int bb = b+width;
        for (int xCoord = a; xCoord < aa; xCoord++) {
            for (int yCoord = b; yCoord < bb; yCoord++) {
                map[xCoord][yCoord] = " ";
            }
        }
    }


Do you think that the program just can't handle jumping around the map so quickly with the while loop and random numbers?
Assuming that the definition of map is something like string **map; or string map[70][45];
make sure that the parts of the array exist. exactly how big is map?
Last edited on
45 by 70

map[45][70];
OH MY GOD YES!! I FIXED IT!! Thank you for asking about the dimensions! You got me thinking and now I fixed it. I had the random numbers generating opposite what they should be.
Topic archived. No new replies allowed.