title: C Program: Do-while loop to run program again not working properly
body: <p>My program is designed to sort records in a text file, but my main issue is getting the main function to ask the user if he would like to run the program again to read and write another file. My program right now, when the user enters 1 to run again, skips the first question to enter the program to read. Why is that? I appreciate the help! Here is my main function only: The program compiles only during the first run.</p>
  <pre><code>int main()
  {
      int    fieldCount = 0;
      int    lineCount = 0;
      char file[STR_LEN];
      char   filepath[STR_LEN];
      char** fields = NULL;
      char** lines = NULL;
      int    recCount = 0;
      Person **sortedRecs = NULL;
      int x;
  
      do {
          printf(&quot;Enter path of the file to read: &quot;);
          gets(filepath);
          printf(&quot;Enter path to copy to: &quot;);
          gets(file);
          fields = readFieldsDynamic(filepath, &amp;fieldCount);
          lines = readLinesDynamic(filepath, &amp;lineCount);
          recCount = getPersons(fields, fieldCount, &amp;sortedRecs);
          if (recCount &amp;&amp; lines &amp;&amp; sortedRecs &amp;&amp; (recCount &lt;= lineCount)) {
              writeRecsToFile(file, sortedRecs, recCount, lines, lineCount);
              printf(&quot;Sorted records are written in %s\n&quot;, file);
          }
          if (fields) {
              freePointerToChars(fields, fieldCount);
          }
          if (lines) {
              freePointerToChars(lines, lineCount);
          }
          if (sortedRecs) {
              freeRecs(sortedRecs, recCount);
          }
          printf(&quot;Enter 1 to run program again: &quot;);
          scanf(&quot;%d%*c&quot;, &amp;x);
      } while (x == 1);
      return 0;
  }
  </code></pre>