title: How to read matrix from stdin and put into 2D array in C
body: <p>Saying that the inputs(stdin) are:</p>
  
  <pre><code>6 4
  0 1 1 1 1 0
  1 0 0 0 0 1
  1 0 0 0 0 1
  0 1 1 1 1 0
  </code></pre>
  
  <p>In the first line, 6 and 4 are width and height respectively.</p>
  
  <p>I did</p>
  
  <pre><code>fgets(buf, sizeof(buf), stdin);
  
  if(strlen(buf) &gt; 4 || strlen(buf) &lt; 4)
  {
      printf("Cannot decode\n");
      return 1;
  }
  </code></pre>
  
  <p>So that if I put the number of integers that is greater than or less than 2 as for the first line(width and height), then press enter, it occurs error.</p>
  
  <p>Next step is to put the rest of the inputs in 2D array, board[height][width].</p>
  
  <p>What I did is:</p>
  
  <pre><code>for(i = 0; i &lt; height; i++)
  {
      for(j = 0; j &lt; width; j++)
      {
          scanf("%d", &amp;input);
  
          board[i][j] = input;
      }
  }
  </code></pre>
  
  <p>But the problem is, the matrix needs to match the width and height.</p>
  
  <p>For example, if I enter 6 and 4 as its width and height respectively,
  and then if I put</p>
  
  <pre><code>0 1 1 1 1 
  </code></pre>
  
  <p>then press enter, it needs to occur an error immediately as the number of integers and the width do not match.</p>
  
  <p>scanf ignores the enter key... so how can I make the program occur an error if I put a number of integers that do not match width and height?</p>