title: Why does scanf fail but fgets works?
body: <p>I am asking the user for input on whether they would like to quit the program or not. There are two snippets. One reads input using the scanf() function, while the second reads input using the fgets() function. Using scanf(), the program goes in an infinite loop. Using fgets(), the program performs as intended. Why does scanf() fail, and fgets() work? How can i correct it so that scanf() will work? Here is the code:</p>
  
  <p><strong>First is with scanf()</strong></p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  
  int main(void)
  {
  
      char yesNo[6];
  
      printf("Enter [quit] to exit the program, or any key to continue");
      scanf("%s", &amp;yesNo[6]);
  
      while (strcmp(yesNo,"quit\n") != 0)
      {
           printf("Enter [quit] to exit the program, or any to continue");
           scanf("%s", &amp;yesNo[6]);
      }
  
      return 0;
  }
  </code></pre>
  
  <p><strong>Second is with fgets()</strong></p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  
  int main(void)
  {
  
      char yesNo[6];
  
      printf("Enter[quit] to exit the program, or any key to continue: ");
      fgets(yesNo, 6, stdin);
  
      while (strcmp(yesNo,"quit\n") != 0)
      {
           printf("Enter [quit] to exit the program, or any key to continue:");
           fgets(yesNo, 6, stdin);
      }
  
      return 0;
  }
  </code></pre>