如何在不扫描enter / space字符的情况下扫描到2d字符数组? [英] How do I scan into a 2d char array without scanning in the enter/space chars?

查看:102
本文介绍了如何在不扫描enter / space字符的情况下扫描到2d字符数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我必须制作一个迷宫程序,并且必须先在迷宫中扫描到二维数组。问题出在我将字符放入数组中,输入字符和空格字符始终占据数组的第一个插槽时。

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

推荐答案

好的,例如 user3121023 已注释,在t之前放置一个空格他在

scanf(%c,& maze [j] [i])中的%c ; 有效!

Alright, so as user3121023 commented, putting a space before the %c in
scanf(" %c",&maze[j][i]); works!


%之前的空格将跳过前导空格,例如空格并输入。

The space before the % will skip leading whitespace such as space and enter.

这篇关于如何在不扫描enter / space字符的情况下扫描到2d字符数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆