2D arrays

hey everyone!!! i want to know how to display a 2D array. all it has is a bunch of #s, letters, and symbols.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

using std::cout;
using std::endl;
using std::string;

int main()
{
    const int rows = 5;
    const int columns = 15;
    char array[rows][columns] = { {'1', 'a', 'z', '_', 'b', '9', '5', '/', 'g', '4', '*', 'q', 'z', '%', '|'},
                                           {'b', '+', '-', '\"', 'a', 'd', '>', 'p', 'y', '^', '(', 'g', 'a', 'a', 'f'}, 
                                           {'1', '_', 'o', 'p', 'h', '7', '~', '_', '[', ':', '#', 'z', '#', ':', 's'},
                                           {'/', 'i', 'a', '*', '-', 't', 'v', 'W', ']', '.', '_', '?', 'z', '_', '~'},
                                           {'l', 't', '|', '_', 'r', '8', '4', '+', '|', 'q', 'z', 'q', 'c', '%', 'g'}};
system("pause");


it is givving me errors on line 11 saying "expected ; before { token" and "expected primary expression before { token"

ps. i know not to use the system pause, it was just the first thing that came to mind.
it compiles with VC++. Maby it's the name 'array' that causes the problem?
For displaying it you should use two nested for loops. One for rows, and one for columns.
Why not an array of strings? Something like:
1
2
3
4
5
    const int rows    = 5;
    const int columns = 15;
    const char arr[][] = { "laz_b95/g4*qz%|",
                           "etc...",
                         };

(Each row would be null terminated.)
Last edited on
ok uuummmm i tried doing what stupebrett said and it gave an error.....

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

using std::cout;
using std::endl;
using std::string;

int main()
{
    const int rows = 5;
    const int columns = 15;
    char array[rows][columns] = { {'1', 'a', 'z', '_', 'b', '9', '5', '/', 'g', '4', '*', 'q', 'z', '%', '|'},
                                           {'b', '+', '-', '\"', 'a', 'd', '>', 'p', 'y', '^', '(', 'g', 'a', 'a', 'f'}, 
                                           {'1', '_', 'o', 'p', 'h', '7', '~', '_', '[', ':', '#', 'z', '#', ':', 's'},
                                           {'/', 'i', 'a', '*', '-', 't', 'v', 'W', ']', '.', '_', '?', 'z', '_', '~'},
                                           {'l', 't', '|', '_', 'r', '8', '4', '+', '|', 'q', 'z', 'q', 'c', '%', 'g'}};
                                           for(int r = 1; r < rows; ++r)
                                           {
                                                 for(int c = ; c < columns; ++c)
                                                 {
                                                       cout << array[rows][columns];
                                                 }
                                           }
system("pause");
}


error is on line 18 and it is:
expected primary expression before ; token
1
2
int r = 1; //on line 16, should be
// int r = 0; 

1
2
int c = ; //on line 18, should be
// int c = 0; 
ooooooooohhhhhhhhh ok but now it compiles and shows nothing but "press any key to continue"
1
2
3
4
5
6
cout << array[rows][columns]; //on line 20, should be
// cout << array[r][c];

//rows and columns are the sizes!
// Trying to get the nth index of a n length array meens you are out-of-bounds!
// Same goes for 2D arrays. 
Last edited on
thanx yall it works great
Topic archived. No new replies allowed.