title: How to check if user input is of correct data type in C
body: <p>Am new to programming so apologies if I've done anything stupid.</p>
  
  <p>I am trying to make a program in C that reads a floating-point number from the user and prints it if it is a valid float in-between the values of 0 and 20, otherwise it will ask again.</p>
  
  <p>The code looks like this:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  int main()
  {
      float a;
      do {
          do {
              printf( "Please input a number (0-20):" );
          } while ( scanf( "%f", &amp;a ) != 1 );
      } while ( ( a &lt; 0 ) || ( a &gt; 20 ) );
      printf("%f\n", a );
      return 0;
  }
  </code></pre>
  
  <p>If I enter a number, it works as intended. However, if I enter anything else (such as a character), it breaks and repeatedly prints out the message.</p>
  
  <p>What have I done wrong? I read in another thread that I can use the return value of scanf to verify that
  the input is of the intended data type but that doesn't seem to work here.</p>