title: C scanf() and fgets() problem
body: <p>I'm trying to read user input and store it as a string including the whitespace.  I did a search for a solution and was pointed to fgets() or scanf(%[^\n], str).  But both these solutions give me an error.  </p>
  
  <p>This is what I have: </p>
  
  <pre><code>//MAX_CHARACTERS is set to 30
  
   scanf("%d", &amp;input);
   if (input == 1){
          int pr;
          char na[MAX_CHARACTERS+1];
          printf("\nEnter the name: ");
          scanf("%[^\t\n]", &amp;na);
          while (strlen(na)&gt;MAX_CHARACTERS){
              printf("\nName is too long, enter new name: ");
              scanf("%[^\t\n]", &amp;na);
          }// end na check
          printf("\nEnter priority: ");
          scanf("%d", &amp;pr);
          while (pr&gt;MAX_PRIORITY || pr &lt;MIN_PRIORITY){
              printf("\nBad priority, enter new priority (0-100): ");
              scanf("%d", &amp;pr);
          }//end pr check
  </code></pre>
  
  <p>It works fine if I use <code>%s</code> in all instances of <code>%[^\t\n]</code>  but when I use <code>%[^\t\n]</code> or </p>
  
  <p><code>fgets(na, 30, stdin)</code>, it skips the first scanf for name and goes straight to "Enter priority: ".  Then when I print, I have a blank name with whatever priority I entered.</p>
  
  <p>EDIT:  Sorry, the missing quotes on the first scanf is a typo.  Not a cause of the problem.  I typed in '1' for the first scanf("%d", input).  </p>
  
  <p><strong>FIXED IT</strong></p>
  
  <p>Since it won't let me post an answer yet,</p>
  
  <p>Someone figured it out.  Incase anyone's still interested, the problem was the first scanf().</p>
  
  <pre><code>scanf("%d", &amp;input);
  </code></pre>
  
  <p>It is leaving a \n in the buffer.  The second one is taking the \n and reading it as an input so it gets skipped.  </p>
  
  <h3>SOLUTION:</h3>
  
  <p>Putting a </p>
  
  <pre><code>fflush(stdin); //right after the if statement seems to have fixed the issue. 
  </code></pre>
  
  <p>Thanks for everyone's help.</p>