Function behaving very strangely

I have tried to build a function to determine which directions the player may travel in (in a console-based game, using an integer array as a map with the map reading the elements north, south, east and west of the player's position (unless no such array element exists, which the program shall test for first)) and print them to the console in a sentence like: "You can move: north, east and south.". I have encountered an error, however: I am using a bool array (named 'DirectionSpecifier') to store which directions the player may travel in and an integer (named 'DirectionSpecifierCount') to determine how many directions the player may travel in (so that the sentence to be printed to the screen can be printed properly). I have discovered, however, that the values in the bool array are (strangely) not always '0' or '1', but have been '255', '96' etc. in a couple of instances (mostly in the first time that the function runs). The problem must be in the code to determine which directions are possible (as shown below):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
void DirectionCheck(MapFunctions& MapObject)
    {
        //Tests if north is a suitable direction (and if it exists).
        if(locx>0)
        {
            if(MapObject.ReadMap(locx-1,locy)!=0)
            {
                DirectionSpecifier[North]=1;
                DirectionSpecifierCount++;
            }
        }
        else
        {
            DirectionSpecifier[North]=0;
        }
        //Tests if east is a suitable direction (and if it exists).
        if(locy<MapObject.getCols())
        {
            if(MapObject.ReadMap(locx,locy+1)!=0)
            {
                DirectionSpecifier[East]=1;
                DirectionSpecifierCount++;
            }
        }
        else
        {
            DirectionSpecifier[East]=0;
        }
        //Tests if south is a suitable direction (and if it exists).
        if(locx<MapObject.getRows())
        {
            if(MapObject.ReadMap(locx+1,locy)!=0)
            {
                DirectionSpecifier[South]=1;
                DirectionSpecifierCount++;
            }
        }
        else
        {
            DirectionSpecifier[South]=0;
        }
        //Test if west is a suitable direction (and if it exists).
        if(locy>0)
        {
            if(MapObject.ReadMap(locx,locy-1)!=0)
            {
                DirectionSpecifier[West]=1;
                DirectionSpecifierCount++;
            }
        }
        else
        {
            DirectionSpecifier[West]=0;
        }
    //... 


Does anybody know what is wrong with this? Is more information necessary?
Thanks!
Your program doesn't assign a default value to a new variable unless it is global,static or constructed:
1
2
3
4
5
6
7
8
int foo; //This will initialize to 0

int main()
{
static int stat; //This will also initialize to 0
int bar; //This will remain uninitialized
return 0;
}


Therefore, the first time around, your array will have been assigned a random memory space, but the data in that memory hasn't change so you might see funny values like that. Also, keep in mind that anything non-0 is considered true. And while this works:
1
2
3
4
//Assuming DirectionSpecifier to be an array of bools
DirectionSpecifier[West]=1;
//This is much clearer to read, and immediately lets you know DirectionSpecifier holds bools:
DirectionSpecifier[West] = true;

Your compiler probably gave you a warning about conversion between types on that one.
Topic archived. No new replies allowed.