问题描述
因此,我必须制作一个迷宫程序,并且必须先在迷宫中扫描到二维数组。问题出在我将字符放入数组中,输入字符和空格字符始终占据数组的第一个插槽时。
So I have to make a maze program, and to start I must scan in the maze to a 2d array. The problem arises when i go to put the chars into the array, when the enter char and the space char always take up the first slot in the array...
这里是我的代码。
int main(){
int row, col;
int i,j,k,l;
scanf("%d", &row);
scanf("%d", &col);
char** maze = (char**) calloc(row, sizeof(char*));
for ( k = 0; k < row; k++ )
{
maze[k] = (char*) calloc(col, sizeof(char));
}
for(i=0;i<row;i++){
for(j=0;j<col;j++){
scanf("%c",&maze[j][i]);
}
}
for(k=0;k<row;k++){
for(l=0;l<col;l++){
printf("%c", maze[k][l]);
}
printf("\n");
}
return 0;
}
输出为:
使用输入字符:
3
3
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xx
xxx
xxx
带空格:
3
3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xx
xxx
xxx
没有任何东西:(这有效)
without any thing: (this one works)
3
3xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
3xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxx
xxx
xxx
推荐答案
好的,例如 ,在t之前放置一个空格他在
scanf(%c,& maze [j] [i])中的
有效!%c
;
Alright, so as user3121023 commented, putting a space before the %c
inscanf(" %c",&maze[j][i]);
works!
这篇关于如何在不扫描enter / space字符的情况下扫描到2d字符数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!