C++ Grid Error

Hello, i'm new to C++, I'm just learning basic C++ and am working on an exercise in which I'm involved in making a simple grid. And although I have a good knowledge concerning arrays and loops, some of the first things I learned about, I seem to be unable to get this grid to work properly. As far as I know the grid should work, however, when I test the program the console somes up with a blank screen when there should be a 7x10 grid. I've pasted samples of others work into the program to see if their code will show a grid and they do. I compare sample of code to what I have pasted, and I can't spot anything to different to seem significant. And now, after a little tweaking to make it work although it should, whenever I execute the program my pc sends an error message, and my program crashes. I'm absolutely clueless of what I've done wrong, I personally think there may be something wrong with the compiler itself which I am using. Here is the code I have typed up for this exercise, and I hope someone can help me with my trouble.
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()
{
    //Declares variables.
    int i, j;
    char grid[i][j];
    
    //Assigns grid variables the '.' character.
    for(i = 0; i < 7; i++)
    {
             for(j = 0; j < 10; j++)
             grid[i][j] = '.';
             }
    
    //Outputs Grid         
    for(i = 0; i < 7; i++)
    {
             for(j = 0; j < 10; j++)
             cout << grid[i][j];
             }
    
    //Waits for input and closes the program.
    cin.get();
    return 0;
    } 

Any suggestions? Thank You.
Well, for starters, you never assigned a value to i and j. And it seems even after I made i = 7 and j = 10, the compiler wouldn't create the grid. I think you would have to make i and j to be constants, but then you couldn't use them in the for loop. So, I just created grid[7][10]. Your output was NOT a grid of 7x10, but one long line. I changed the loop a bit to print 7 separate lines. Hope this helps out.

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
// Grid.cpp : main project file.

#include "stdafx.h"
#include <iostream>

using namespace std;

int main()
{
    //Declares variables.
    int i, j;
	char grid[7][10];
    
    //Assigns grid variables the '.' character.
    for(i = 0; i < 7; i++)
		{
            for(j = 0; j < 10; j++)
            grid[i][j] = '.';
         }
    cout << "\n\t\t<< A 7 by 10 Grid >>"; // Give screen a title
	cout << "\n\n";
    //Outputs Grid         
    for(i = 0; i < 7; i++)
    {
		cout << "\t\t"; // Move grid over two tabs
		{
			for(j = 0; j < 10; j++)
             cout << grid[i][j];
        }
		cout << "\n";
	}
    
    //Waits for input and closes the program.
    cin.get();
    return 0;
    } 
The values in your array declaration have to be constants. May I suggest something such as:

1
2
#define GRID_X_SIZE 7
#define GRID_Y_SIZE 10 


So your array declaration can look like this:

1
2

char grid[GRID_X_SIZE][GRID_Y_SIZE];


And your loops can look like this:

1
2
3
4
5
6
7
for(int x = 0; x < GRID_X_SIZE; x++)
{
    for(int y = 0; y < GRID_Y_SIZE; y++)
    {
    ...
    }
}


There's an array tutorial on this site that may be helpful: http://www.cplusplus.com/doc/tutorial/arrays/
Thank you very much I finally realized what I was doing wrong :S, as I can see from your posts I realized the main Problem I had was that I never gave the grid array a true value when it was declared and therefore, for for some reason when I executed the program it crashed. Once I did that I saw that the output was only a long horizontal line, and not the grid I was intending for. After that I realized that the reason it was outputing this line was because I had put an endl command within the loop the cout command was placed and therefore created a newline not for every row, but for every character which was output into the console. So I simply moved that into the 'outer' loop which was affecting the x coordinate of the grid.
This is my final sample including the fixes I had made thanks to you guys.
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
#include <iostream>
using namespace std;

int main()
{
    //Declares variables.
    int i, j;
	char grid[7][10]; // Fix 1
    
    //Assigns grid variables the '.' character.
    for(i = 0; i < 7; i++)
		{
            for(j = 0; j < 10; j++)
            {
            grid[i][j] = '.';
            }
         }
    //Outputs Grid         
    for(i = 0; i < 7; i++)
    {
 
			for(j = 0; j < 10; j++)
			{
             cout << grid[i][j];
             }
             
             cout << endl; // Fix 2
	}
    
    //Waits for input and closes the program.
    cin.get();
    return 0;
    } 

Thank You very much, I do hope not all C++ is this confusing! :S
Topic archived. No new replies allowed.