Matrix of Char in C - Using Scanf or getchar

Pedro Lino

i'm having a litle trouble with input info on a matrix

i want to build a simple wordsearch game. The user tells the dimension of the matrix to the wordsearch and inputs the characters and i want to print it just to see if everything is okey with the info.

This is my code:

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]);
}

If i input something like

2 3
ABC
DEF

The output should be:

This is your Matrix:
ABC
DEF

But my output when i print is something like

This is your Matrix:

ABC
D

It first gives a "\n" then prints but not complete.

What am i doing wrong? Please take in consideration that i should only be using functions like scanf and getchar to build the matrix.

chux - Reinstate Monica

OP: "What am i doing wrong?"
A: scanf("%c", .. is reading '\n' left over from the previous scanf ("%d%d"... To avoid that and other white-spaces, consume them by pre-pending a ' ' in the "%c" format.

1) Check scanf() results.

2) Use Space before "%c" to consume whitespace, especially the previous line's \n.

3) Use main() correctly.

4) Better to use fputs() or puts() when simply printing a string

Edit: Meet ability to enter a scant line

#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;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Using Scanf after getchar?

From Dev

using getchar() in C to send input to a char array

From Dev

Trouble with using getchar() instead of scanf()

From Dev

Differences between scanf and getchar in C

From Dev

Using scanf() on *char[]

From Dev

C scanf char array

From Dev

Scanf a char in C with an example

From Dev

char Input in C by scanf

From Dev

C: Using scanf to accept a predefined input length char[]

From Dev

Trouble using char in scanf in a structure

From Dev

using getchar() in C; does it move to the next char every time I use it? Including within assignment operations?

From Dev

C: scanf for char not working as expected

From Dev

Both scanf("%c", x) and x=getchar aren't waiting for input

From Dev

Devowelling an input using getchar() in C

From Dev

Usage of scanf ... getchar

From Dev

confused about getchar and scanf

From Dev

the input buffer in getchar() and scanf

From Dev

skipped trouble with getchar and scanf

From Dev

confused about getchar and scanf

From Dev

Char matrix in C++?

From Dev

Using scanf() for arrays in C

From Dev

Using pointers to return char and float values from scanf back to main function C

From Dev

Creating a gets function that returns a char* on return pressed using a getchar function

From Dev

getchar not taken in consideration after scanf

From Dev

getchar not taken in consideration after scanf

From Dev

Removing multiple blanks using putchar and getchar in C

From Dev

C - Swap chars using getchar and putchar

From Dev

Removing multiple blanks using putchar and getchar in C

From Dev

Do scanf and getchar handle stream differently?