Printing a 2d char array

  • Follow


I am trying to print a 2d character array and its just not working.
What am I doing wrong? I think i may need a pointer, but I am not sure
just how to do that.  All that it prints now, is that it starts on line
two and just prints one character at a time, which isn't right.  Thanks
in advance


char maze[][12]=
{{ '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' },
{ '#', '.', '.', '.', '#', '.', '.', '.', '.', '.', '.', '#' },
{ '.', '.', '#', '.', '#', '.', '#', '#', '#', '#', '.', '#' },
{ '#', '#', '#', '.', '#', '.', '.', '.', '.', '#', '.', '#' },
{ '#', '.', '.', '.', '.', '#', '#', '#', '.', '#', '.', '.' },
{ '#', '#', '#', '#', '.', '#', '.', '#', '.', '#', '.', '#' },
{ '#', '.', '.', '#', '.', '#', '.', '#', '.', '#', '.', '#' },
{ '#', '#', '.', '#', '.', '#', '.', '#', '.', '#', '.', '#' },
{ '#', '.', '.', '.', '.', '.', '.', '.', '.', '#', '.', '#' },
{ '#', '#', '#', '#', '#', '#', '.', '#', '#', '#', '.', '#' },
{ '#', '.', '.', '.', '.', '.', '.', '#', '.', '.', '.', '#' },
{ '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' }};

for ( unsigned int row =0; row < 12; row++)
{
        for (unsigned int col = 0; col < 12; col++)
                {
                cout << maze[row][col] << ' ' ;

                cout << endl;
                }


      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply alisha427 (3) 2/15/2006 1:28:25 PM

"shadow427" <alisha427@netzero.com> writes:

> I am trying to print a 2d character array and its just not working.

What is not working? I.e. what do you expect to happen, and what does
happen effectively?

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply Thomas 2/15/2006 5:25:17 PM


* "shadow427" <alisha427@netzero.com>
| All that it prints now, is that it starts on line two and just
| prints one character at a time, which isn't right.
--<snip-snip>--
| for ( unsigned int row =0; row < 12; row++)
| {
|         for (unsigned int col = 0; col < 12; col++)
|                 {
|                 cout << maze[row][col] << ' ' ;
| 
|                 cout << endl;
|                 }

Should be obviuos, no?  Currently you're printing an endl after each
column.  Move the printing of 'endl' _after_ the col block.

R'

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply Ralf 2/15/2006 5:29:10 PM

> * "shadow427" <alisha427@netzero.com>
> | All that it prints now, is that it starts on line two and just
> | prints one character at a time, which isn't right.
> --<snip-snip>--
> | for ( unsigned int row =0; row < 12; row++)
> | {
> |         for (unsigned int col = 0; col < 12; col++)
> |                 {
> |                 cout << maze[row][col] << ' ' ;
> |
> |                 cout << endl;
> |                 }

Ralf Fassel wrote:
> Should be obviuos, no?  Currently you're printing an endl after each
> column.  Move the printing of 'endl' _after_ the col block.

Yes, Shadow, Ralf is right... in other words, check where your braces
are.


      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply Allan 2/16/2006 2:01:34 AM

You call cout<<endl after each character (in the inner loop).

for ( unsigned int row =0; row < 12; row++)
{
        for (unsigned int col = 0; col < 12; col++)
                {
                cout << maze[row][col] << ' ' ;
//              cout << endl;  // <-- remove from here
                }
        cout << endl; // <-- to here
}

By the way, if you add a column with zero-terminators, you can print
each row in single expression:

char maze[12][13] = {
  "#.#....##", // automatic way to make zero-terminated string
  {'#',......'#',0} // manual
  .....
};

for(int row=0; row<12; ++row)
  cout << maze[row] << endl;


      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply Kodt 2/16/2006 2:03:12 AM

Kodt wrote:
>
> By the way, if you add a column with zero-terminators, you can print
> each row in single expression:
>
> char maze[12][13] = {
>   "#.#....##", // automatic way to make zero-terminated string
>   {'#',......'#',0} // manual
>   .....
> };
>
> for(int row=0; row<12; ++row)
>   cout << maze[row] << endl;

Just note that string literals should not be modified. :)
In other words, these two are not equivalent.

// maze[i][j] cannot be modified
char maze[12][13] = {
   "#.#....##",
   // ...
};

// maze[i][j] can be modified
char maze[12][13] = {
   {'#',......'#',0},
   // ...
};

-- 
Seungbeom Kim

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply Seungbeom 2/18/2006 12:07:17 PM

In article <dt63hf$biu$1@news.Stanford.EDU>, Seungbeom Kim 
<musiphil@bawi.org> writes
>Just note that string literals should not be modified. :)
>In other words, these two are not equivalent.
>
>// maze[i][j] cannot be modified
>char maze[12][13] = {
>   "#.#....##",
>   // ...
>};
>
>// maze[i][j] can be modified
>char maze[12][13] = {
>   {'#',......'#',0},
>   // ...
>};
Would you like to try to justify that? Leave out the distraction of a 
second dimension and look at:

char message[] = "help";
and
char message[] = {'h', 'e', 'l', 'p', '\0'};

and explain why you think those are not equivalent. Of course neither is 
equivalent to:

char * message = "help";

which despite the lack of a const qualifier results in message pointing 
to a non-modifiable array of char.

-- 
Francis Glassborow      ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects


      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply Francis 2/19/2006 3:08:06 PM

Francis Glassborow wrote:
> In article <dt63hf$biu$1@news.Stanford.EDU>, Seungbeom Kim 
> <musiphil@bawi.org> writes
>> Just note that string literals should not be modified. :)
>> In other words, these two are not equivalent.
>>
>> // maze[i][j] cannot be modified
>> char maze[12][13] = {
>>   "#.#....##",
>>   // ...
>> };
>>
>> // maze[i][j] can be modified
>> char maze[12][13] = {
>>   {'#',......'#',0},
>>   // ...
>> };
> Would you like to try to justify that? Leave out the distraction of a 
> second dimension and look at:
> 
> char message[] = "help";
> and
> char message[] = {'h', 'e', 'l', 'p', '\0'};
> 
> and explain why you think those are not equivalent. Of course neither is 
> equivalent to:
> 
> char * message = "help";
> 
> which despite the lack of a const qualifier results in message pointing 
> to a non-modifiable array of char.

Oops, that was my mistake; I thought that the first example
had an array of pointers, not an array of arrays of chars.

Thanks for pointing it out. :)

-- 
Seungbeom Kim

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply Seungbeom 2/20/2006 10:56:41 AM

On 15 Feb 2006 08:28:25 -0500, "shadow427" <alisha427@netzero.com>
wrote:

>I am trying to print a 2d character array and its just not working.
>What am I doing wrong? I think i may need a pointer, but I am not sure
>just how to do that.  All that it prints now, is that it starts on line
>two and just prints one character at a time, which isn't right.  Thanks
>in advance
>
>
>char maze[][12]=
>{{ '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' },
>{ '#', '.', '.', '.', '#', '.', '.', '.', '.', '.', '.', '#' },
>{ '.', '.', '#', '.', '#', '.', '#', '#', '#', '#', '.', '#' },
>{ '#', '#', '#', '.', '#', '.', '.', '.', '.', '#', '.', '#' },
>{ '#', '.', '.', '.', '.', '#', '#', '#', '.', '#', '.', '.' },
>{ '#', '#', '#', '#', '.', '#', '.', '#', '.', '#', '.', '#' },
>{ '#', '.', '.', '#', '.', '#', '.', '#', '.', '#', '.', '#' },
>{ '#', '#', '.', '#', '.', '#', '.', '#', '.', '#', '.', '#' },
>{ '#', '.', '.', '.', '.', '.', '.', '.', '.', '#', '.', '#' },
>{ '#', '#', '#', '#', '#', '#', '.', '#', '#', '#', '.', '#' },
>{ '#', '.', '.', '.', '.', '.', '.', '#', '.', '.', '.', '#' },
>{ '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' }};
>
>for ( unsigned int row =0; row < 12; row++)
>{
>        for (unsigned int col = 0; col < 12; col++)
>                {
>                cout << maze[row][col] << ' ' ;
>
Move this line:
>                cout << endl;
>                }
here:
>
(and add a '}' here.

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply SB 3/3/2006 12:48:12 PM

8 Replies
475 Views

(page loaded in 0.124 seconds)

Similiar Articles:













7/23/2012 4:30:57 AM


Reply: