2d grid array

This is supposed to be a grid and it's supposed to have different letters on the grid in different directions. But only one letter is showing up on my code
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
  #include <iostream>
#include <vector>
#include <ctime>
#include <cstdlib>
using namespace std;
const int M = 8;
const int N = 7;

void initialize_grid(char grid[M][N]);
void print_grid(char grid[M][N]);
bool can_go_left(int i, int j);
bool can_go_right(int i, int j);
bool can_go_up(int i, int j);
bool can_go_down(int i, int j);

int main()
{
    srand(time(NULL));
    char grid[M][N];
    // Start out with '.' (DOT) in each space
    for(int i = 0; i < M; i++)
    {
        for(int j = 0; j < N; j++)
        {
            grid[i][j] = '.';
        }
    }
    initialize_grid(grid);
    print_grid(grid);

}
void initialize_grid(char grid[M][N])
{
    int CurrentI = 0;
    int CurrentJ = 0;
    char currentChar = 'A';
    while(currentChar <= 'Z' && CurrentI < M
          && CurrentJ < N)
    {
        grid[CurrentI][CurrentJ] = currentChar;
        currentChar++;

        switch(rand()%4)
        {
            bool can_go_left(int i, int j);
        case 0: // left
            if( CurrentJ > 0 && grid[CurrentI][CurrentJ-1] == '.')
            {
                CurrentJ--;
                break;
            }

            bool can_go_right(int i, int j);
        case 1: // right
            if(CurrentJ < N-1 && grid[CurrentI][CurrentJ+1] == '.')
            {
                CurrentJ--;
                break;
            }

            bool can_go_down(int i, int j);
        case 2: // down
            if(CurrentI > 0 && grid[CurrentI-1][CurrentJ] == '.')
            {
                CurrentI--;
                break;
            }

            bool can_go_up(int i, int j);
        case 3: // up
            if(CurrentI > M - 1 && grid[CurrentI+1][CurrentJ] == '.')
            {
                CurrentI++;
                break;
            }


    }
    }

}
void print_grid(char grid[M][N])
{
      for(int i = 0; i < M; i++) {
        for(int j = 0; j < N; j++) {
            cout << grid[i][j];
        }
        cout << endl;
}
}
lines 45,53,61,69: These are function declarations, not function calls. If you meant these as function declarations, there is no need for them here as you've already declared them at lines 11-14.

See
http://www.cplusplus.com/forum/beginner/204593/
Please continue that thread.

From the meaning of the word "initialize" and your comment on line 20, one would assume you are supposed to assign dots ('.') in initialize_grid, - not try to put lots of other routines in there.
1
2
3
4
5
6
7
void initialize_grid(char grid[M][N])
{
   for(int i = 0; i < M; i++)
   { 
      for(int j = 0; j < N; j++) grid[i][j] = '.';
   }
}

Topic archived. No new replies allowed.