title: How to validate the input type to receive integer only?
body: <p>I only want to receive integer from the user and I tried doing as the code below but it doesn't seems to be quite right.</p>
  
  <pre><code>int num;
  
  printf("What is the ID?\n&gt;&gt; ");
          while(!scanf("%d", &amp;num)){
              // consume new line
              getchar();
              printf("\nInvalid ID please try again...\n\n");
              printf("What is the ID?\n&gt;&gt; ");
          }
  </code></pre>
  
  <p>If I only input <code>a</code> into the <code>scanf</code>, it works fine, but when I input <code>ab</code> into the <code>scanf</code>, it loops 2 times and if i input 3 alphabet it will loops 3 times. Why is it?</p>
  
  <pre><code>What is the ID?
  &gt;&gt; a
  
  Invalid ID please try again...
  
  What is the ID?
  &gt;&gt; abc
  
  Invalid ID please try again...
  
  What is the ID?
  &gt;&gt;
  Invalid ID please try again...
  
  What is the ID?
  &gt;&gt;
  Invalid ID please try again...
  
  What is the ID?
  &gt;&gt;
  </code></pre>