title: Avoiding `fgets()` double Enter hits
body: <p>I am trying to collect data from user and I want to accomplish the task using <code>fgets()</code>.</p>
  
  <p>In my <code>main.c</code> file:</p>
  
  <pre><code>do
      {
          user = ask_user_info();
  
          // ... Code exporting data to file ...
  
          free(user);
  
          fprintf(stdout, "Do you want to add another user?\nChoice: ");
          scanf("%c[^\n]", &amp;choice);
  
      } while (choice == 'y');
  </code></pre>
  
  <p>Here are the functions I have written to get the job done:</p>
  
  <pre><code>UserData *ask_user_info()
  {
      char firstname[STRLEN];
      char lastname[STRLEN];
      char username[STRLEN];
      char password[STRLEN];
      char email[STRLEN];
  
      fprintf(stdout, "First Name: ");
      get_user_input(firstname);
      flush_stdin();
  
      fprintf(stdout, "Last Name: ");
      get_user_input(lastname);
      flush_stdin();
  
      fprintf(stdout, "Username: ");
      get_user_input(username);
      flush_stdin();
  
      fprintf(stdout, "Password: ");
      get_user_input(password);
      flush_stdin();
  
      fprintf(stdout, "Email: ");
      get_user_input(email);
      flush_stdin();
  
      return fill_fields(firstname, lastname, username, password, email);
  }
  
  void get_user_input(char *input)
  {
      int length;
      char *buffer = (char *) malloc (STRLEN * sizeof(char));
      (*buffer) = '\0';
  
      if (fgets(buffer, STRLEN, stdin) != NULL)
      {
          length = strlen(buffer)-1;
          buffer[length] = '\0';
          strncpy(input, buffer, length+1);
      }
  
      free(buffer);
  }
  
  UserData *fill_fields(const char firstname[], const char lastname[], 
                          const char username[], const char password[], const char email[])
  {
      UserData *user = (UserData *) malloc (sizeof(UserData));
  
      user-&gt;firstname = (char *) malloc (strlen(firstname) * sizeof(char));
      strncpy(user-&gt;firstname, firstname, strlen(firstname));
      user-&gt;lastname = (char *) malloc (strlen(lastname) * sizeof(char));
      strncpy(user-&gt;lastname, lastname, strlen(lastname));
      user-&gt;username = (char *) malloc (strlen(username) * sizeof(char));
      strncpy(user-&gt;username, username, strlen(username));
      user-&gt;password = (char *) malloc (strlen(password) * sizeof(char));
      strncpy(user-&gt;password, password, strlen(password));
      user-&gt;email = (char *) malloc (strlen(email) * sizeof(char));
      strncpy(user-&gt;email, email, strlen(email));
  
      return user;
  }
  
  void flush_stdin()
  {
      int c;
      while ((c = getchar()) != '\n' &amp;&amp; c != EOF);
  }
  </code></pre>
  
  <p>Everything compiles and everything seems to work properly, but when the program asks for user input, (most of the times) it requires the user to hit <kbd>Enter</kbd> twice to move on to the next input.</p>
  
  <p>On one hand I know that this behaviour is caused by the <code>flush_stdin()</code> function. On the other I can't get rid of the <code>flush_stdin()</code> function because it makes sure that no input field gets skipped. If I did the program would output something like <code>First Name: Last Name:</code> </p>
  
  <p>How do I avoid the double hit and make sure all input is collected? </p>