Need help creating a grid

Hi. I am barely new to C++.

Right now I am trying to make a 9X9 grid that fills @ # $ % randomly.

I wrote down some codes, but I am constantly getting error LNK2019 and LNK1120.

How can I deal with this?

Here is the code that I wrote.

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
  #include <iostream>
#include <string.h>
#include <windows.h>

using namespace std;

	void drawGrid(char grid[9][9])
	{
		cout << " " << grid[0][0] << " | " << grid[0][1] << " | " << grid[0][2] << " | " << grid[0][3] << " | " << grid[0][4] << " | " << grid[0][5] << " | " << grid[0][6] << " | " << grid[0][7] << " | " << grid[0][8] << endl;
		cout << "---|---|---|---|---|---|---|---|---\n";
		cout << " " << grid[0][0] << " | " << grid[1][1] << " | " << grid[1][2] << " | " << grid[1][3] << " | " << grid[1][4] << " | " << grid[1][5] << " | " << grid[1][6] << " | " << grid[1][7] << " | " << grid[1][8] << endl;
		cout << "---|---|---|---|---|---|---|---|---\n";
		cout << " " << grid[0][0] << " | " << grid[2][1] << " | " << grid[2][2] << " | " << grid[2][3] << " | " << grid[2][4] << " | " << grid[2][5] << " | " << grid[2][6] << " | " << grid[2][7] << " | " << grid[2][8] << endl;
		cout << "---|---|---|---|---|---|---|---|---\n";
		cout << " " << grid[0][0] << " | " << grid[3][1] << " | " << grid[3][2] << " | " << grid[3][3] << " | " << grid[3][4] << " | " << grid[3][5] << " | " << grid[3][6] << " | " << grid[3][7] << " | " << grid[3][8] << endl;
		cout << "---|---|---|---|---|---|---|---|---\n";
		cout << " " << grid[4][0] << " | " << grid[4][1] << " | " << grid[4][2] << " | " << grid[4][3] << " | " << grid[4][4] << " | " << grid[4][5] << " | " << grid[4][6] << " | " << grid[4][7] << " | " << grid[4][8] << endl;
		cout << "---|---|---|---|---|---|---|---|---\n";
		cout << " " << grid[5][0] << " | " << grid[5][1] << " | " << grid[5][2] << " | " << grid[5][3] << " | " << grid[5][4] << " | " << grid[5][5] << " | " << grid[5][6] << " | " << grid[5][7] << " | " << grid[5][8] << endl;
		cout << "---|---|---|---|---|---|---|---|---\n";
		cout << " " << grid[6][0] << " | " << grid[6][1] << " | " << grid[6][2] << " | " << grid[6][3] << " | " << grid[6][4] << " | " << grid[6][5] << " | " << grid[6][6] << " | " << grid[6][7] << " | " << grid[6][8] << endl;
		cout << "---|---|---|---|---|---|---|---|---\n";
		cout << " " << grid[7][0] << " | " << grid[7][1] << " | " << grid[7][2] << " | " << grid[7][3] << " | " << grid[7][4] << " | " << grid[7][5] << " | " << grid[7][6] << " | " << grid[7][7] << " | " << grid[7][8] << endl;
		cout << "---|---|---|---|---|---|---|---|---\n";
		cout << " " << grid[8][0] << " | " << grid[8][1] << " | " << grid[8][2] << " | " << grid[8][3] << " | " << grid[8][4] << " | " << grid[8][5] << " | " << grid[8][6] << " | " << grid[8][7] << " | " << grid[8][8] << endl;
	}

	void randomFill(char grid[9][9], int j, int i)
	{
		for (int i = 0; i < 9; i++)
		{
			for (int j = 0; j < 9; j++)
			{
				grid[i][j] = ((rand() % 4) == 0) ? '@' : '#'; '$'; '%';
			}
		}
		drawGrid(grid);
	}
Your code has no main() function.

Program execution starts at main(), it must be present.
within main you can then call your other functions as required.

Thank you for the reply.

After uploading this question, I have noticed that the code has no main function.

I edited the code, but now it does not give me the 4 characters... It only gives me 2 @ and #.

int main()
{
char grid[9][9];
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
grid[i][j] = ((rand() % 4) == 0) ? '@': '#'; '$'; '%';
}
}
drawGrid(grid);


return 0;
}


How can I fix this?
This line:
 
    grid[i][j] = ((rand() % 4) == 0) ? '@': '#'; '$'; '%';

.. well actually it is three lines:
1
2
3
    grid[i][j] = ((rand() % 4) == 0) ? '@': '#';  // choose @ or #
    '$';        // do nothing
    '%';        // do nothing 

... is selecting from the two possible choices.
Remember, the ? operator in this syntax is simply a concise way to write an if/else statement, that's why it gives an either/or choice.


Instead I'd suggest something like this:
1
2
3
4
5
6
7
8
9
10
    char fill[] = "@#$%";             // list of possible characters 
    int len = strlen(fill);           // count how many characters 

    for (int i = 0; i < 9; i++)
    {
        for (int j = 0; j < 9; j++)
        {
            grid[i][j] =  fill[rand() % len];   // choose one of characters
        }
    }

Last edited on
Topic archived. No new replies allowed.