title: Keyboard input makes function not work properly - C
body: <p>I have the following code:</p>
  
  <p><strong>browser.h</strong></p>
  
  <pre><code>#include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  
  typedef struct webpage {
      struct webpage *prev;
      struct webpage *next;
      char* url;
  } Webpage;
  
  Webpage *current = NULL;
  
  void free_mem(Webpage *p) {
      if (p-&gt;next != NULL) {
          free_mem(p-&gt;next);
      }
      free(p);
  }
  
  char * add_url(char *url) {
      Webpage *p = malloc(sizeof(Webpage));
      p-&gt;url = url;
      if (current != NULL) {
          if (current-&gt;next != NULL) {
              free_mem(current-&gt;next);
          }
          current-&gt;next = p;
          p-&gt;prev = current;
  
      } else {
          p-&gt;prev = NULL;
      }
      p-&gt;next = NULL;
      current = p;
  }
  
  void list_history() {
      Webpage *temp = current;
      printf("--- HISTORIAL ---\n");
      while(temp != NULL) {
          printf("%s\n", temp-&gt;url);
          temp = temp-&gt;prev;
      }
      printf("--- FIN ---\n");
  }
  
  void nav_back() {
      if (current-&gt;prev != NULL) {
          current = current-&gt;prev;
      } else {
          printf("IGNORADO\n");
      }
  }
  
  void nav_forward() {
      if (current-&gt;next != NULL) {
          current = current-&gt;next;
      } else {
          printf("IGNORADO\n");
      }
  }
  </code></pre>
  
  <p><strong>main.c</strong></p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include "browser.h"
  
  int main() {
      add_url("www.google.com");
      add_url("www.something.com");
      add_url("www.a.com");
      add_url("www.b.com");
      add_url("www.c.com");
      add_url("www.d.com");
      add_url("www.e.com");
      list_history();
      nav_back();
      nav_back();
      list_history();
      nav_forward();
      list_history();
      add_url("www.new-url.com");
      list_history();
      system("pause"); // this line only works on Windows
      return 0;
  }
  
  //int main (int argc, char *argv[]) {
  //    add_url(argv[1]);
  //    char *option = "Z";
  //    char input_text[2002];
  //    while (1) {
  //        printf("Ingrese comando(A,S,U,L,Q) (URL actual = %s): ", current-&gt;url);
  //        fgets(input_text, 2002, stdin);
  //        option = strtok(input_text, "\n ");
  //        if (strcmp(option, "A") == 0) {
  //            nav_back();
  //        } else if (strcmp(option, "S") == 0) {
  //            nav_forward();
  //        } else if (strcmp(option, "U") == 0) {
  //            char *url = strtok(NULL, "\n");
  //            printf("La URL es: %s\n", url);
  //            add_url(url);
  //        } else if (strcmp(option, "L") == 0) {
  //            list_history();
  //        } else if (strcmp(option, "Q") == 0) {
  //            break;
  //        } else {
  //            printf("OPCION NO VALIDA\n");
  //        }
  //    }
  //}
  </code></pre>
  
  <p>The first <code>main</code> function works flawlessly and it does everything it has to do, but the second <code>main</code> function doesn't work properly (i.e. I add some urls and it seems to work fine, but when I try to list_history nothing appears)</p>
  
  <p>I can't find the reason why the second <code>main</code> isn't working. If you can help I'll be very thankful</p>
  
  <p>Sample execution:</p>
  
  <pre><code>Ingrese comando(A,S,U,L,Q) (URL actual = (null)): U a.com
  La URL es: a.com
  Ingrese comando(A,S,U,L,Q) (URL actual = a.com): U b.com
  La URL es: b.com
  Ingrese comando(A,S,U,L,Q) (URL actual = b.com): U c.com
  La URL es: c.com
  Ingrese comando(A,S,U,L,Q) (URL actual = c.com): U d.com
  La URL es: d.com
  Ingrese comando(A,S,U,L,Q) (URL actual = d.com): U e.com
  La URL es: e.com
  Ingrese comando(A,S,U,L,Q) (URL actual = e.com): U f.com
  La URL es: f.com // works fine up to here
  Ingrese comando(A,S,U,L,Q) (URL actual = f.com): L // L should show a list of all URLS written above
  --- HISTORIAL ---
  
  
  
  
  
  
  --- FIN --- // You can see that history has the space for the URLs but none was printed
  Ingrese comando(A,S,U,L,Q) (URL actual = ): A // now it doesn't show current URL "URL actual" any more. Notice before it was showing current after I added one.
  Ingrese comando(A,S,U,L,Q) (URL actual = ): Q
  </code></pre>