title: Wait for press enter in C inside a while loop?
body: <p>I'm writing a C program and I need to wait for the user to press any key to continue. When I use <code>getchar();</code> it waits for the <kbd>Enter</kbd> key to be pressed. But when I use it inside a <code>while</code> loop, it doesn't work. How can I make my code wait for any key to be pressed to continue the loop?</p>
  
  <p>Here is my code sample. I am using GNU/Linux.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include&lt;stdlib.h&gt;
  int main()
  {
      int choice;
      while(1) {
          printf("1.Create Train\n");
          printf("2.Display Train\n");
          printf("3.Insert Bogie into Train\n");
          printf("4.Remove Bogie from Train\n");
          printf("5.Search Bogie into Train\n");
          printf("6.Reverse the Train\n");
          printf("7.Exit");
          printf("\nEnter Your choice : ");
          fflush(stdin);
          scanf("%d",&amp;choice);
  
          switch(choice)
          {
              case 1:
                  printf("Train Created.");
                  break;
              case 2:
                  printf("Train Displayed.");
                  break;
              case 7:
                  exit(1);
              default:
                  printf("Invalid Input!!!\n");
          }
  
          printf("Press [Enter] key to continue.\n");
          getchar();
      }
  
      return 0;
  }
  </code></pre>