[英]Sudoku Solver Input
I'm creating a sudoku solver in C and having trouble obtaining user input. 我正在用C创建一个数独求解器,无法获取用户输入。 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. 我编写的代码不会将数据输入游戏板,但是如果我将Game_Buffer[counter]
更改为Game_Buffer[i]
它将仅输入9个字符。 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. counter++
在循环后执行。 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. 添加所有缺少的括号,然后检查问题是否仍然存在。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.