Displaying an Array in a nested for loop

Oct 29, 2012 at 8:27pm
Hello, I am attempting to make a Sudoku solver, and i am having issues getting the grid to display, i have the values saved as a [9][9] array, and i am trying to get them to display using a nested for loop that should go through each group and display them, but it wont display them, the program does compile and run without any errors.
here is the code for the display
1
2
3
4
5
6
7
8
9
10
11
void drawGrid()
{
    for (int f = 0; f > 9; f++)
    {
        for (int g = 0; g > 9; g++)
        {
            cout << grid[g][f] << " ";
        }
        cout << "\n";
    }
}

and here is the project
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <iostream>
#include <windows.h>
using namespace std;

int grid[9][9];
void drawGrid();
void defGrid();

int main()
{
    defGrid();
    int x, y;
    int a;
    cout << "Hello, lets solve a sudoku shall we?" << endl;
    do
    {
        drawGrid();
        cout << "Enter the grid coords x,y then the number 1-9, or enter 0 0 0 to begin solving" << endl;
        cin >> x;
        cin >> y;
        cin >> a;
        x = x - 1;
        y = y - 1;
        if (x > 9 || x < 0)
        {
            x = 0;
        }
        if (y > 9 || y < 0)
        {
            y = 0;
        }
        if (a > 9 || a < 1)
        {
            a = 0;
        }
        grid[x][y] = a;
        system("cls");
    }
    while(a != 0);
    do
    {
        cout << "Please select an option." << endl;
        cout << "1 : Run the Row Checker" << endl;
        cout << "2 : Run the Column Checker" << endl;
        cout << "3 : Run the Area Checker" << endl;
        cout << "4 : Exit" << endl;
        cin >> a;
        system("cls");
        switch (a)
        {
        case 1:
            break;
        case 2:
            break;
        case 3:
            break;
        case 4:
            cout << "Good Bye." << endl;
            return 0;
            break;
        }
    }
    while(true);
}

void defGrid()
{
    for (int y = 0; y > 9; y++)
    {
        for (int x = 0; x > 9; x++)
        {
            grid[x][y] = 0;
        }
    }
}


i moved the display to top section for easier viewing.
Oct 29, 2012 at 9:12pm
In your nested for loops you're using > 9 instead of < 9. Put < 9 and it should work (haven't looked through the rest of your code though).
Oct 30, 2012 at 12:09am
Yeah, that was the issue, thank you very much.
Topic archived. No new replies allowed.