Array

Hello, i planned on trying to make a small game where a character would move through out a 2d array. For whatever reason i cant get the array to print. Ive looked at different posts here and stack exchange/overflow and have seen many problems such as mine. But from what i see on the responses theres nothing wrong. It compiles just fine, but nothing appears whenever it compiles. It just says that it returned 0. Help?!

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

using namespace std;

int main() {

char grid[5][5] = {{'1','2','3','4','5'},
                  {'1','2','3','4','5'},
                  {'1','2','3','4','5'},
                  {'1','2','3','4','5'},
                  {'1','2','3','4','5'}};

for (int i; i < 5; i ++) {   //had to initialize i and j they didnt have a value assigned to them 

    for (int j; j < 5; j ++){

       cout << grid[i][j]; //had to add " " to create a space between each line
       
    }

return 0; //had to move this so it didnt terminate the first for loop

}


}
Last edited on
Try removing the "return 0;" from within the first for-loop.

Now the first for-loop starts, the second for-loop is executed, and before the second for loop can be executed for 1=1, you tell the main function to return, which terminates your program.

Kind regards, Nico
I moved the return out of the first for loop and im still not getting any output to the screen. Any other suggestions?
You haven't initialized your variables in line 13 and 15

13
14
15
for (int i=0; i < 5; i ++) {

    for (int j=0; j < 5; j ++){
Try flushing the output stream, with either flush, or endl.

And if your on Windows put a cin.get() before the return statement.
Isnt doing int i, and int j inside the for loop initializing the variables? @shadder


And how would flushing work when i am not taking any data from a file? @jlb Maybe i dont fully understand how that works then if thats not the case then correct me if im wrong. :P. Also the cin.get() function isnt doing anything but waiting for me to press a key and then it just gives me the same problem as before.

Im on code::blocks if thats of any use?

Thanks for all the recomendations!

Heres my edited 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
#include <iostream>

using namespace std;

int main() {

char grid[5][5] = {{'1','2','3','4','5'},
                  {'1','2','3','4','5'},
                  {'1','2','3','4','5'},
                  {'1','2','3','4','5'},
                  {'1','2','3','4','5'}};

for (int i; i < 5; i ++) {

    for (int j; j < 5; j ++){

       cout << grid[i][j] << endl;

    }



}
cin.get();

return 0;

}
Last edited on
Isn't doing int i, and int j inside the for loop initializing the variables?

No. You're declaring i and j, but you're not giving them an initial value. Variables are not initialized to 0 be default.

You're not getting any output because your for loops are not executing. Because i and j are not initialized, they contain garbage which is larger than 5, therefore the termination condition for your loops is met immediately.

And how would flushing work when i am not taking any data from a file?
jlb is talking about flushing the cout buffer. This has nothing to do with reading data from a file. You probably want to do
1
2
//  line 20
   cout << endl;

This will cause your numbers to be displayed in 5 rows instead of all in a single row.

Last edited on
And how would flushing work when i am not taking any data from a file?

You flush an output stream not an input stream. Remember that the output streams are normally buffered, which means that the output will not appear until the output buffer is filled. Using flush, or endl forces the buffer to be flushed thereby printing the output to the console.

Im on code::blocks if thats of any use?

Not really of any use in this case, although knowing your operating system would be useful.

Isnt doing int i, and int j inside the for loop initializing the variables?

No, you need to initialize the variables.

Try something like:
1
2
3
4
5
6
for (int i = 0; i < 5; i++) {
    for (int j = 0; j < 5; j++){
       cout << grid[i][j] << ' ';
    }
    cout << endl;
}

Notice how index variables are initialized int the for loop initialization section.

I never realized that when i declared the variable that it never actually initialized it a value, guess thats what i get for learning off web forums :P.

I finally got it to output to the console but how do i get it to display to look like a grid how i have it typed in the array? It currently just displays it in a line.


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() {

char grid[5][5] = {{'1','2','3','4','5'},
                  {'1','2','3','4','5'},
                  {'1','2','3','4','5'},
                  {'1','2','3','4','5'},
                  {'1','2','3','4','5'}};

                  int i;
                  int j;

for (i = 0; i < 5; i ++) {

    for (j = 0; j < 5; j ++){

       cout << grid[i][j];
    }



}

return 0;

}



Last edited on
1
2
3
4
5
6
 int i = 0;
                  int j = 0;

for (int i; i < 5; i ++) {

    for (int j; j < 5; j ++){


you are re declaring i and j
Maybe im missing something that youll said or misunderstood what you meant.

You are.

You are still not initializing the loop index variables. You have variables with the same names within different scopes. The loop variables are not initialized, even though you declared and initialized two variables with the same names in the outer scope (line 13 and 14).



I realized that as soon as i posted it, and edited my comment. Look at the code again youll see i fixed. I also edited my question. Sorry for that.
I also edited my question. Sorry for that.

Look at the code I posted previously.

how do i get it to display to look like a grid how i have it typed in the array?

See what I said above about line 20.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;

int main() 
{   
char grid[5][5] = {{'1','2','3','4','5'},
                  {'1','2','3','4','5'},
                  {'1','2','3','4','5'},
                  {'1','2','3','4','5'},
                  {'1','2','3','4','5'}};

    for (int i = 0; i < 5; i ++)            //  Declare and initialize i
    {   for (int j = 0; j < 5; j ++)    //  Declare and initialize j
            cout << grid[i][j] << " ";  //  Add a space between numbers
        cout << endl;                   //  Flush each line
    }
    return 0;
}
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
Press any key to continue . . .
Last edited on
Thanks for that jlb, why is it that i have to have the << ' ';? Really basic question but im curious.
why is it that i have to have the << ' ';?

You want a space between each number? Then you need to output a space.
Topic archived. No new replies allowed.