Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C中字符的矩阵-使用Scanf或getchar_C - Fatal编程技术网

C中字符的矩阵-使用Scanf或getchar

C中字符的矩阵-使用Scanf或getchar,c,C,我在矩阵输入信息方面有点问题 我想建立一个简单的文字搜索游戏。用户将矩阵的维度告知wordsearch并输入字符,我想打印它,看看信息是否一切正常 这是我的代码: void main (){ int nl, nc, i,j; scanf ("%d %d", &nl,&nc); //Input number of lines and collums of the matrix char matrix [nl] [nc]; for (i=0;i<nl;i++) f

我在矩阵输入信息方面有点问题

我想建立一个简单的文字搜索游戏。用户将矩阵的维度告知wordsearch并输入字符,我想打印它,看看信息是否一切正常

这是我的代码:

void main (){
int nl, nc, i,j;

scanf ("%d %d", &nl,&nc); //Input number of lines and collums of the matrix

char matrix [nl] [nc];

for (i=0;i<nl;i++)
    for (j=0;j<nc;j++)
            scanf("%c",&matrix[i][j]);   //Input matrix

printf("This is your matrix:\n");
for (i = 0; i < nl; i++)
    for (j = 0; j < nc; j++)
       printf("%c", matrix [i][j]);
}
输出应为:

This is your Matrix:
ABC
DEF
但我打印时的输出类似于

2 3
ABC
DEF
This is your Matrix:

ABC
D
它首先给出一个“\n”,然后打印,但不完整


我做错了什么?请考虑我应该只使用scanf和getchar之类的函数来构建矩阵。

您需要跳过换行符。如果您只知道scanf,则可以按如下方式执行:

char dummy;
scanf("%d %d", &nl,&nc);
scanf("%c", &dummy);  \\ newline is consumed here.
每行后面的换行符也是一样的,因此对于读取矩阵:

for (i=0;i<nl;i++) {
    for (j=0;j<nc;j++)
        scanf("%c",&matrix[i][j]);   //Input matrix
    scanf("%c", &dummy);
}

对于(i=0;i试试这个,也许会有帮助:

int main()
{
int nl, nc, i,j;

scanf ("%d %d", &nl,&nc); //Input number of lines and collums of the matrix

char* matrix=(char*)malloc(sizeof(char)*nl*nc);

char* s=(char*)malloc(sizeof(char)*nl);


for (i=0;i<nl;i++)
{
    scanf("%s",s);
    for (j=0;j<nc;j++)
        matrix[i*nl+j]=s[j];
}
printf("This is your matrix:\n");
for (i = 0; i < nl; i++)
{
    for (j = 0; j < nc; j++)
       printf("%c", matrix [i*nl+j]);
    printf("\n");
}


  return 0;
}
intmain()
{
int nl、nc、i、j;
scanf(“%d%d”,&nl,&nc);//输入矩阵的行数和共线数
char*矩阵=(char*)malloc(sizeof(char)*nl*nc);
char*s=(char*)malloc(sizeof(char)*nl);
对于(i=0;i试试这个:

void main (){
int nl, nc, i,j;

scanf ("%d %d\n", &nl,&nc); //Input number of lines and collums of the matrix

char matrix [nl] [nc];

for (i=0;i<nl;i++){
    for (j=0;j<nc;j++)
            scanf("%c",&matrix[i][j]);   //Input matrix
    scanf("\n");
}
printf("This is your matrix:\n");
for (i = 0; i < nl; i++)
    for (j = 0; j < nc; j++)
       printf("%c", matrix [i][j]);
}
void main(){
int nl、nc、i、j;
scanf(“%d%d\n”,&nl,&nc);//输入矩阵的行数和共线数
字符矩阵[nl][nc];
对于(i=0;i
#包括
#包括
#包括
int main(){
int nl、nc、i、j;
scanf(“%d%d\n”,&nl,&nc);//输入矩阵的行数和共线数
char*矩阵=(char*)malloc(sizeof(char)*nl*nc);

对于(i=0;i),因为您最初将此标记为C++,这应该有帮助:

unsigned int rows;
unsigned int columns;
cin >> rows;
cin >> columns;
std::vector<char> matrix(rows * columns);
for (unsigned int r = 0; r < rows; ++r)
{
  std::string row_text;
  getline(cin, row_text);
  for (unsigned int c = 0; c < columns; ++c)
  {
    matrix.push_back(row_text[c]);
  }
}

for (unsigned int row = 0; row < rows; ++row)
{
  for (unsigned int col = 0; col < columns; ++col)
  {
    cout << matrix[row * columns + col];
  }
  cout << "\n";
}
无符号整数行;
无符号整数列;
cin>>行;
cin>>列;
std::向量矩阵(行*列);
for(无符号整数r=0;r
答:
scanf(“%c”…
正在读取上一个
scanf(“%d%d”)的剩余
\n'
为避免该空格和其他空格,请使用
“%c”
格式的
'
预挂起的

1) 检查
scanf()
结果

2) 在
“%c”
之前使用空格来使用空格,尤其是前一行的
\n

3) 正确使用
main()

4) 简单打印字符串时,最好使用fputs()或put()

编辑:满足输入尺寸界线的能力

#include<stdio.h>
#include<stdlib.h>

void ConsumeToEOL(void) {
  int ch;
  do {
    ch = getchar();
  } while (ch != '\n' && ch != EOF);
}

int main() {
  int nl, nc, i, j;
  // Space between "%d %d" not really needed
  if (scanf("%d%d", &nl, &nc) != 2) {
    fputs("Bad number Input\n", stdout);
    exit(1);
  }
  ConsumeToEOL();

  char matrix[nl][nc];
  for (i = 0; i < nl; i++) {
    for (j = 0; j < nc; j++) {
      int ch = getchar();
      if (ch == '\n' || ch == EOF)
        break;
      matrix[i][j] = (char) ch;
    }
    if (j == nc) ConsumeToEOL();
    for (; j < nc; j++) {
      matrix[i][j] = 0;
    }
  }
  // Better to use fputs() or puts() when simply printing a string
  fputs("This is your matrix:\n", stdout);
  for (i = 0; i < nl; i++) {
    for (j = 0; j < nc; j++) {
      if (matrix[i][j]) {
        printf("%c", matrix[i][j]);
      }
    }
    // Add EOL
    fputs("\n", stdout);
  }
  return 0;
}
#包括
#包括
无效TOEOL(无效){
int-ch;
做{
ch=getchar();
}而(ch!='\n'&&ch!=EOF);
}
int main(){
int nl、nc、i、j;
//实际上不需要“%d%d”之间的空间
如果(扫描频率(“%d%d”、&nl和&nc)!=2){
fputs(“错误的数字输入”,标准输出);
出口(1);
}
TOEOL();
字符矩阵[nl][nc];
对于(i=0;i
#包括
int main(){
int nl、nc、i、j;
scanf(“%d%d\n”,&nl,&nc);//输入矩阵的行数和共线数
字符矩阵[nl][nc];
对于(i=0;i试试这个:

#include <stdio.h>
int main()
{
    int r, c, i, j;
    printf("Rows = ");
    scanf("%d", &r);
    printf("Col = ");
    scanf("%d", &c);
    char a[r][c];
    for (i = 0; i < r; i++)
    {
        for (j = 0; j < c; j++)
        {
            printf("Alphabet in row %d and column %d = ", i + 1, j + 1);
            scanf(" %c", &a[i][j]);
        }
    }
    printf("\n Given Matrix :\n");
    for (i = 0; i < r; i++)
    {
        printf("\n");
        for (j = 0; j < c; j++)
        {
            printf(" %c ", a[i][j]);
        }
    }
    printf("\n");
}
// End of main.
#包括
int main()
{
int r,c,i,j;
printf(“Rows=”);
scanf(“%d”、&r);
printf(“Col=”);
scanf(“%d”、&c);
字符a[r][c];
对于(i=0;i
这个肯定很好用

*注: 注意-->scanf(“%c”、&a[i][j]);
在这里,在%c之前有一个空格,以便使用空格。

我希望这能为您找到解决方法。 (与您发布的代码相同,只需稍加修改)

void main(){
int nl、nc、i、j;
scanf(“%d%d”,&nl,&nc);//输入矩阵的行数和列数
字符矩阵[nl][nc];

对于(i=0;i
void main
应该是
int main
。您还应该确保输入成功。嗨@chris,我知道main,但是我的gcc使用void编译,所以没有问题。我的输入不是这样成功的吗?不要使用
scanf()
。通过
fgets()
读取行,然后用
sscanf()解析缓冲区
strtol()
(仅使用scanf和getchar等函数是一种不幸但人为的约束。)然后检查
scanf()的结果。如果是2,一切都好。@ThomasMatthews谁使用过“strings”这个词这里?你是唯一一个谈论这个的人…我的代码没有任何字符串提示。我们谈论的是2D字符数组。你好,这里@Marian…我可以把它放在for cicle中吗?比如,jhi@Zindarod,我使用“New”时出错。它说它没有声明。@PedroLino你必须包含stdlib.h for malloc()。无法使用malloc抱歉。我还没有了解stackClose man close之外的内存分配。它会在开始时删除\n,但不会打印F。仅显示“DE”在打印时它会删除\n,它也会扰乱输入中的行数。如果我想输入2行,它要求3,至少不能使用Malc,抱歉。它不会。我会从标签中删除C++,这样你就不再告诉我关于iTi@ CuxI的事了。
#include <stdio.h>


int main (){
        int nl, nc, i,j;

        scanf ("%d %d\n", &nl,&nc); //Input number of lines and collums of the matrix

        char matrix [nl][nc];

        for (i=0;i<nl;i++) {
                char c;
                for (j=0;j<nc;j++) {
                        scanf("%c",&matrix[i][j]);   //Input matrix
                }
                scanf("%c", &c);
        }

        printf("This is your matrix:\n");
        for (i = 0; i < nl; i++) {
                for (j = 0; j < nc; j++)
                        printf("%c", matrix [i][j]);
                printf("\n");
        }

        return 0;
}
#include <stdio.h>
int main()
{
    int r, c, i, j;
    printf("Rows = ");
    scanf("%d", &r);
    printf("Col = ");
    scanf("%d", &c);
    char a[r][c];
    for (i = 0; i < r; i++)
    {
        for (j = 0; j < c; j++)
        {
            printf("Alphabet in row %d and column %d = ", i + 1, j + 1);
            scanf(" %c", &a[i][j]);
        }
    }
    printf("\n Given Matrix :\n");
    for (i = 0; i < r; i++)
    {
        printf("\n");
        for (j = 0; j < c; j++)
        {
            printf(" %c ", a[i][j]);
        }
    }
    printf("\n");
}
// End of main.
void main (){
int nl, nc, i,j;

scanf ("%d %d", &nl,&nc); //Input number of lines and columns of the matrix

char matrix [nl] [nc];

for (i=0;i<nl;i++)
    for (j=0;j<nc;j++)
        scanf("%s",&matrix[i][j]);   //Input matrix ***GIVE INPUT IN FORM OF STRING THAT IS '%s' ***

printf("This is your matrix:\n");
for (i = 0; i < nl; i++)
    {
        for (j = 0; j < nc; j++)
            printf("%c", matrix [i][j]);
        printf("\n");                      // USE '\n' FOR NEXT FOR SUCCESSFUL OUTPUT OF YOUR CODE
    }
}