title: fgets() / scanf() do-while loop
body: <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  
  int main()
  {
  
      char strA[10];
  
      int i, j;
  
  
      do {
  
          j = 0;
  
          memset(&amp;strA,'\0', sizeof(strA));
  
          printf("Please enter your password: ");
          fgets (strA,10,stdin);
  
          printf("\n%s\n\n",strA);
  
          if (strlen(strA) &gt; 8) {
              printf("That password is too long\n");
          }
          else {
              j++;
          }
  
      } while (j&lt;1);
  return 0;
  }
  </code></pre>
  
  <p>Hello.  I am running <code>fgets()</code> inside a dowhile loop.  I first test to see if an inputted string is too long, and then prompt to enter the string again if the string is too long (by letting the dowhile start over).  The problem is that the carry over from a string that is too long (the extra characters that get cut from <code>fgets()</code> being set to 10) gets inputted in the second, third, and so on iteration of the dowhile loop until eventually the remainder of the string satisfies the else statement, and the dowhile terminates.  I need each iteration of the dowhile loop to be a fresh start where a string is manually entered.  Can anyone help?</p>
  
  <p>replace:</p>
  
  <pre><code>fgets (strA,10,stdin);
  </code></pre>
  
  <p>with:</p>
  
  <pre><code>scanf("%10s", strA);
  </code></pre>
  
  <p>same problem.</p>