# Weird problem with c++



## GSquadron (Aug 24, 2011)

Hi guys!
I am having a bit of trouble getting to know why and where is the problem here.

```
for( i = 1; i < 9; i++){
    int x = rand() % 9;
    int y = rand() % 9;
    row[x][y] = choice; // <-- i think this choice here cannot be replaced
    cin >> choice;
    }
```
When it generates the sudoku and u add numbers,
it always shows a number 0 which i don't even know why happens. 
The only problem i think is, the first choice cannot be replaced.


----------



## ctrain (Aug 25, 2011)

need more than that, what are you getting choice from?

and are you declaring i before the for loop? don't need to do that in C++ (or C99)


----------



## GSquadron (Aug 25, 2011)

Actually this is the code in a more complex way. It does 2 times the for in the second for
The choice is for 9 rows i think?
And no i am not declaring both of i.

```
for( i = 0; i < 9; i++ ){
        int x = rand() % 8;
        int y = rand() % 8;
        row[x][y] = 0;}

    for (i = 0; i < 9; i++)
    {
        for (j = 0; j < 9; j++)
        {
            cout << " " << row[i][j];
        }
        cout << endl;
    }
```


----------



## ctrain (Aug 25, 2011)

row is going to have garbage data in it if you didn't fill it with something before hand. you're only filling certain spots with a 0 in the first loop.

int row[9][9] = { 0 }; 
will effectively memset the entire thing to 0.

for the second loop you're probably reading garbage numbers.


----------



## GSquadron (Aug 25, 2011)

So you mean this way is done?

```
for( i = 0; i < 9; i++ ){
        int x = rand() % 8;
        int y = rand() % 8;
        row[x][y] = 0;}

        int row[9][9] = { 0 }; 

    for (i = 0; i < 9; i++)
    {
        for (j = 0; j < 9; j++)
        {
            cout << " " << row[i][j];
        }
        cout << endl;
    }
```


----------



## ctrain (Aug 25, 2011)

if you just had:

int row[9][9];

then it would have just been filled with garbage characters initially, it would have just looked like
-12389184
49128942
6868
0
12938129
3585971
-19
0

etc etc after your loop.


i'm just taking stabs at what you're showing me, paste everything. i still don't know where choice comes in and how row is defined.


----------



## GSquadron (Aug 25, 2011)

LOL finally u think at least 
The number 0 is not a garbage i think, 
as it always shows 0 and not any other random garbage


----------



## ctrain (Aug 25, 2011)

i lied i'm still confused.


how do you declare row?

do you fill it with anything before this?

```
for(int i = 0; i < 9; i++)
{
        int x = rand() % 8;
        int y = rand() % 8;
        row[x][y] = 0;
}
}
```

how does choice come in?

what is happening vs what you expect?


i'm guessing choice is just random numbers to add to the generated puzzle, then you'll check to make sure it's still solvable?


----------



## GSquadron (Aug 25, 2011)

OMG i feel like i know better c++ than u.
There is not even needed that man, the problem is 100% inside that code!(at least i think so...)
anyway i am trying to answer your questions as clear as possible:
Choice is integer.
I expect that the sudoku, after the user puts in the numbers, is filled with numbers
from 1-9. BUT! after the user fills in the numbers, it has a final 0 which is WIERD
And the row is declared row[9][9] and not with #define


----------



## GSquadron (Aug 25, 2011)

NVM i found the stupid problem:

from:

```
for( i = 1; i < 9; i++){
    int x = rand() % 9;
    int y = rand() % 9;
    row[x][y] = choice; // <-- i think this choice here cannot be replaced
    cin >> choice;
    }
```
to:

```
for( i = 1; i < 9; i++){
    int x = rand() % 9;
    int y = rand() % 9;
    cin >> choice;
    row[x][y] = choice;
    }
```


----------



## Kreij (Aug 25, 2011)

The follwing code you are using will not give you what you expect

```
for( i = 0; i < 9; i++ )
{
        int x = rand() % 9;
        int y = rand() % 9;
        row[x][y] = 0;
}
```

Since you are picking random numbers for row and col placement there is no guarrantee that the numbers will not repeat when you mod them with 9.
Even if by some chance they do not repeat you are still only filling 9 of the 81 array positions with a value of zero. The rest of the array position will have some indeterminant value unless they were set previously.


----------



## GSquadron (Aug 25, 2011)

In fact i dont really want a "REAL" random 0. Just to fill in some values for example to 9.
But i know that, cuz if i want to make all the sudoku with 0 i need to put in 300 and not 81.
No prob cuz i dont really need a super sudoku, just one who generates and can be played in.
The real problem now is how to make it generate the sudoku with 0 first and than to cin!


----------



## ctrain (Aug 25, 2011)

Aleksander Dishnica said:


> NVM i found the stupid problem:
> 
> from:
> 
> ...



i figured you were doing something with it earlier, that's why i asked for more code, you weren't being clear. i literally mentioned the choice thing in my first reply.


----------



## GSquadron (Aug 25, 2011)

The code was not clear not me


----------

