简体   繁体   中英

Sudoku Solver Input

I'm creating a sudoku solver in C and having trouble obtaining user input. The code that I've written doesn't input the data into the game board, but if I change Game_Buffer[counter] to Game_Buffer[i] it inputs the data but only 9 characters. I am aware of why. I just wanted to see if their were problems in other areas.

My primary question is: Why is the method I'm using not placing the user input data into the game board array?

#include <stdio.h>
#include <string.h>
#define CELL 81

int main()
{
    // Banner
    printf("\t\t\t\tSudoku Solver\n");
    printf("\t\t\t***************************\n");

    //initialize variables
    char Game_Board[9][9];
    int i,j;
    char Game_Buffer[CELL];
    int counter = 0;

    printf("Please enter the numbers of the board * denotes a blank space\n");
    fgets(Game_Buffer,CELL,stdin);

    for(i=0;i<strlen(Game_Buffer);i++)
        printf("%c", Game_Buffer[i]);

    while(counter < 81)
    {
        for(i=0; i<9; i++)
            for(j=0; j<9; j++)

                Game_Board [i][j] = Game_Buffer [counter];
                counter++;
    }

    printf("%d\n", counter);
    printf("\t\t\t\t The Board\n");

    for( i=0; i<9; i++)
        for( j=0; j<9; j++)
        {

            if( j % 3 == 0)
                printf("|");
            printf("%c", Game_Board[i][j]);
            if(j==8)
                printf("|\n");

        }

    return 0;
}

The counter++ executes after the loop. I have idented the code to show what I mean..

for(i=0; i<9; i++)
  for(j=0; j<9; j++)
    Game_Board [i][j] = Game_Buffer [counter];
counter++;

You are updating all cells with the same value.

You probably should use brackets at first place.

    for(i=0; i<9; i++)
    {
        for(j=0; j<9; j++)
        {
            Game_Board [i][j] = Game_Buffer [counter];
            counter++;
        }
    }

Add all missing brackets and check if your issue still exists.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM