Why doesn't this char array work?

Hi.
I'm trying to do a program that prints out a full block of '#'. The block size can the user decide. When it prints out the array it's just a bunch of other characters.

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
#include <iostream>

using namespace std;

int main()
{
    int block_size_y;
    int block_size_x;
    cout << "Enter block size(x): "; cin >> block_size_x;
    cout << "Enter block size(y): "; cin >> block_size_y;

    char block_1[block_size_y][block_size_x];

    for(int y = 0; y < block_size_y; y++)
    {
        for(int x = 0; x < (block_size_x - 1); x++)
        {
            block_1[y][x] = '#';
        }
    }

    {
        for(int i = 0; i < block_size_y; i++)
        {
            cout << block_1[i] << endl;
        }
    }


}
You are only outputting one index. You need to do this:

1
2
3
4
5
6
7
8
for(int i = 0; i < block_size_y; i++)
{
     for(int y = 0; y < block_size_x; y++)
     {
            cout << block_1[i][y];
     }
     cout<<endl;
}
What you are doing is illegal in the C++ standard. Arrays can only be a constant (fixed) value, variables undeclared with the const keyword can change. What you want to do is dynamically allocate an array size of block_size_y and block_size_x.
Last edited on
First of all this statement

char block_1[block_size_y][block_size_x];

is invalid from the point of view of the C++ Standard.

As for your problem then write

1
2
3
4
5
6
7
8
    for(int y = 0; y < block_size_y; y++)
    {
        for(int x = 0; x < (block_size_x - 1); x++)
        {
            block_1[y][x] = '#';
        }
        block_1[y][block_size_x - 1] = '\0';
    }
Last edited on
Is this what you were trying to do?

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
#include <iostream>

using namespace std;

int main()
{
    int block_size_y;
    int block_size_x;
    cout << "Enter block size(x): "; cin >> block_size_x;
    cout << "Enter block size(y): "; cin >> block_size_y;

    char block_1[block_size_y][block_size_x];

    for(int y = 0; y < block_size_y; y++)
    {
        for(int x = 0; x < block_size_x ; x++)
        {
            block_1[y][x] = '#';
        }
    }

       for(int i = 0; i < block_size_y; i++)
        {
            for (int j = 0; j < block_size_x; j++)
                cout << block_1[i][j];
           cout << endl;
        }
    


}


This will work, but if you are allowed to, it would probably be a good ideal to use a dynamic two-dimensional array
Last edited on
C++ doesn't support variable length arrays.

You don't need an array of any kind for printing.

Thanks for all the help. I guess I just assumed that it would work as it has worked on this 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
#include <iostream>
#include "windows.h"

using namespace std;

const int gameSquare = 20;

char gameMap1[gameSquare][gameSquare] = {

    "###################",
    "#                 #",
    "#                 #",
    "#                 #",
    "#                 #",
    "#                 #",
    "#                 #",
    "#                 #",
    "#     ### ###     #",
    "#     #+   +#     #",
    "#     #######     #",
    "#                 #",
    "#                 #",
    "#                 #",
    "#                 #",
    "#        W        #",
    "#                 #",
    "#                 #",
    "#                 #",
    "###################",

};

int main()
{
     for(int i = 0; i < gameSquare; i++)
             {
                 switch(i)
                 {
                     default:
                         cout << gameMap1[i] << endl;
                     break;
                 }
             }
}
That is quite different. The array size is fixed and known at compile time. Each row of the matrix is initialized with a cstring; there is an invisible null character at the end of each string. Note how there are only 19 printable chars per row.

gameMap1[i] has type 'char *'. cout << gameMap1[i] copies consecutive characters from memory to cout until it finds a null character. Your version had "#" all over and no nulls.

1
2
3
4
5
for ( int row = 0; row < block_size_y; ++row )
  {
    std::cout << std::setfill( '#' ) << std::setw( block_size_x ) << "#\n";
  }
std::cout << std::flush;
Topic archived. No new replies allowed.