-1

I have a 2D array that I am using as my template for a Sudoku puzzle, it is a 9x9 array that I intend to have filled with numbers after getting input from the user.

I have not tried an awful lot as of yet because I am lost and haven't found and useful resources

#include <cstdlib>
#include <iostream>
#include <stdio.h>      /* printf, scanf, puts, NULL */
#include <stdlib.h>     /* srand, rand */
#include <time.h>       /* time */
#include <cstdio>
#include <cstring>
#include <fstream>
using namespace std;
#define N rand() % 10
/* #define N2 rand() % 10 */


int main(){
    for (int i = 0; i < 10 ; i++){
        srand (time(NULL));
        int c1;
        int c2;
        int c3;

        cout << "Insert number to fill: " ;
        cin >> c3;
        /*c1 = N ;
        c2 = N ;*/
        /*cin >> c1;
        cin >> c2;*/
        /* cout << N << '\n'; /* << N2 << '\n'; */
        int sudoku[][9] = { {0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0},};
        int width = 9, height = 9;

        sudoku[N][N] = c3;

        cout << sudoku << '\n';
        /*  cout << rand()%10;*/
        for (int i = 0; i < height; ++i)
        {
            for (int j = 0; j < width; ++j)
            {
                cout << sudoku[i][j] << ' ';
            }
            cout << endl;
        }
    }
    return 0;
}

This is the code, it prints a 9x9 set of 0's and when I input a number it shows correctly, however when my code prompts for the next number to input, the array no longer has the previously input number. I think I have to save the array each time, maybe to a file, but I'm not entirely sure.

ufoxDan
  • 609
  • 4
  • 13
Aidan Hart
  • 15
  • 3
  • *This is obviously a cut down array but it gets the idea across.* -- No it doesn't get the idea across. What are you actually trying to accomplish? Start a program with results from a previous run of a program? – PaulMcKenzie Nov 11 '19 at 23:43
  • There we go, didn't think I'd have to add the rest of the code, I wrongly thought my explanation was enough. – Aidan Hart Nov 11 '19 at 23:56
  • I'm new to CPP and haven't had any issues in my prior CPP programs so forgive me for looking for help this time. – Aidan Hart Nov 12 '19 at 02:30

1 Answers1

2

You are reinitializing your sudoku array on each iteration of the for loop, so each time it loops through every value is set to 0 again. Move the initialization outside of the loop:

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

for (int i = 0; i < 10 ; i++){

       ...
}

You can just use the one zero for initializing all in a 2D array to 0 (note this won't work for non-zero initial values see this answer for info on that)

ufoxDan
  • 609
  • 4
  • 13