title: My code starts to be executed even before getting the first necessary input
body: <p>This is about a cricket match, and I am going to be given some inputs. The first input is going to be a number, which says how many inputs it will be followed by, and each of them would be strings. The characters in each string will represent the outcomes of balls played in that match. The following shows the possible outcomes:</p>
  <p>Outcome: Description</p>
  <p>N: No ball,
  W: Wide ball,
  D: Dead ball,
  O: Out,
  0-6: Runs scored.</p>
  <p>For example, an input might be:
  3
  W123NW6WD64
  444WO213
  444</p>
  <p>Now for each of these 3 series of balls, I have to find out the numbers of legal balls played in that series. The output for this input is expected to be:
  6
  7
  3</p>
  <p>I tried to build the algorithm, and everything seemed to be fine, but my code is giving output to a string before it reads one. that is, for the input above, my code outputs:
  0
  6
  7.</p>
  <p>Here is my code:</p>
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  
  void balls (char* ptr);
  
  int main()
  {
      int count;
      scanf (&quot;%d&quot;, &amp;count);
  
      for (int i = 0; i &lt; count; i++)
      {
          char str[100];
          gets (str);
          balls (str);
      }
  }
  
  void balls (char* ptr)
  {
      int balls = 0;
      int length = strlen (ptr);
  
      for (int k = 0; k &lt; length; k++)
      {
          switch (*(ptr + k))
          {
          case 'N':
              break;
  
          case 'W':
              break;
  
          case 'D':
              break;
  
          case '\0':
              break;
  
          default:
              balls++;
              break;
          }
      }
  
      printf (&quot;%d\n&quot;, balls);
  }
  </code></pre>
  <p>How do you think I can fix this? Thanks in advance!</p>