i suspect this is the most simple array problem here but

shurley this should ouput a table, i feel like maybe i have forgotton something
fundemental, instead i get flat lines no big beautiful table.

did you know victorians made skirts for tables because they thought the bare legs were immoral!

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

using namespace std;
char z = '*';
char x = ' ';
char brdray [8] [8]= { {z,z,z,z,z,z,z,z} ,{z,x,x,x,x,x,x,z} ,{z,x,x,x,x,x,x,z}, {z,x,x,x,x,x,x,z} ,{z,x,x,x,x,x,x,z} ,{z,x,x,x,x,x,x,z}, {z,x,x,x,x,x,x,z}, {z,z,z,z,z,z,z,z} };
void getboard();
int main ()
{

getboard ();

     return 0;
}

void getboard()
{
for (int a = 0;a<7;a++)
 {
     for (int b =0;b<7;b++)
        cout << brdray [a][b];
 }

}
You need to to output a new line character when you want a new line.
cout << '\n';
carriage return (\n)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void getboard()
{
for (int a = 0;a<7;a++)
 {
     for (int b =0;b<7;b++)
        cout << brdray [a][b];

        cout<<"\n"; 

/*newline character is needed, you can also use a Line feed operator                 
                   cout<<endl;
Its important to know where exactly it is needed, it is needed after the inner for loop ie after each row is printed.*/  
 }

}
Last edited on
You have 8 items in the array, so the limit in for loops should be 8 as well.

Your initialisation of the array doesn't have to be on 1 line.

1
2
3
4
5
6
7
8
char brdray [8] [8]= { {z,z,z,z,z,z,z,z} ,
                                    {z,x,x,x,x,x,x,z} ,
                                    {z,x,x,x,x,x,x,z}, 
                                    {z,x,x,x,x,x,x,z} ,
                                    {z,x,x,x,x,x,x,z} ,
                                    {z,x,x,x,x,x,x,z},
                                    {z,x,x,x,x,x,x,z},
                                    {z,z,z,z,z,z,z,z} }


There is a bit of an unwritten rule that code shouldn't be longer than 80 chars per line. This saves having to scroll sideways all the time, and is easier in case some one wants to print it.

Edit: You need single quotes around each char 'z' and 'x'

HTH
Last edited on
is okay z and x are char names not the values...just with endline, i get iether lon ****** ** etc or lots of lines eg
*
*
*
*
*
*
*

like that, i also get amazing grids of * that repeats, as cool as it is its not what im after
Ok, I missed the variables x & z.

Can you post your new code again, I don't see why it shouldn't work given the suggestions already.



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

using namespace std;




char you = '^';

char z = '*';
char x = ' ';
char brdray [8] [8]= { {z,z,z,z,z,z,z,z} ,{z,x,x,x,x,x,x,z} ,{z,x,x,x,x,x,x,z},
{z,x,x,x,x,x,x,z} ,{z,x,x,x,x,x,x,z} ,{z,x,x,x,x,x,x,z},
{z,x,x,x,x,x,x,z}, {z,z,z,z,z,z,z,z} };


void getboard();
int main ()
{

getboard ();

     return 0;
}

void getboard()
{
for (int a = 0;a<8;a++)
 {
     for (int b =0;b<8;b++)
        cout << brdray [a][b]<<endl;
 }

}
Last edited on
been trying to create a class that has the functions in it to get a chars position and then initialise it to a new one... think i need to keep practicing before i move to allegro
1
2
3
4
5
6
7
8
9
void getboard()
{
for (int a = 0;a<8;a++)
 {
     for (int b =0;b<8;b++)
        cout << brdray [a][b]<<endl;
 }

}


The Line feed operator is not to be appended after the cout<<brdray[a][b];

cout<<endl; must be placed after the inner for loop, ie

1
2
3
4
5
6
7
8
9
10
11
12
13
void getboard()
{
for (int a = 0;a<8;a++)
 {
     for (int b =0;b<8;b++)
     {             
      cout << brdray [a][b]; //the braces r not necessary since its a single stat
     }
cout<<endl;
}

}

So you can also write

1
2
3
4
5
6
7
8
9
10
11
12
void getboard()
{
for (int a = 0;a<8;a++)
 {
     for (int b =0;b<8;b++)
                  
      cout << brdray [a][b] 
     
cout<<endl;
}

}
Last edited on
It works wich is fantastic and thank you...but i dont understand it yet, i will mark as solved when i do.
You are welcome :D, but firstly you must understand how nested for-loop works
1
2
3
4
5
6
7
8
9
for(int i=0; i<5; i++)
{
   for(j=0;j<5; j++)
   {
      cout<<"*";   
   }
cout<<endl;
}

for every value of i (from 0 to 4), you run through all values of j.

Outer for-loop
Initialization of i=0, condition i<5 is satisfied hence it executes the statements in the loop which is a for-loop

Inner for-loop:
j=0, condition j<5 is satisfied, hence it executes the statement inside the
brace (Note:for single statement , braces are not required)
The statement is to print a *

Now j is incremented j=1 condition j<5 is satisfied, it prints a *.

Now j is incremented j=2 condition j<5 is satisfied, it prints a *.

Now j is incremented j=3 condition j<5 is satisfied, it prints a *.

Now j is incremented j=4 condition j<5 is satisfied, it prints a *.

Now j is incremented j=5 condition j<5 is not satisfied, the control comes
out of the inner for-loop and executes the next statement which is to print
a new line.

We go to the Outer-loop
i is incremented i=2, i<5 is satisfied and again the inner for-loop is executed.. everything is repeated for every value of i <5

Hence the output is
*****
*****
*****
*****

1
2
3
4
5
6
7
for(int i=0; i<5; i++)
{
   for(j=0;j<5; j++)
    {   
      cout<<"*"<<endl; 
    }
}

Incase of this, since printing of * and a newline belongs to one particular statement hence for every * printed a newline is kept

Hope it helps









Last edited on
@Dala Darko (3)

Just 1 small thing - can you use code tags - the <> button on the right.

Good work on your help to the OP. Cheers :=D
Done :D
Topic archived. No new replies allowed.