title: Questions about validating user input in Objective-C, number vs string
body: <p>Why 'exactly' does this code loop endlessly if you enter a non number character?</p>
  
  <p>The first question comes about because I want to learn good defensive coding. Does anyone know a good way to check user input? My google-fu failed me. Some people seemed to be of the opinion that if I specify <code>%f</code> in <code>scanf</code> that I am 'demanding' a <code>float</code>; I verified this, in a way, by printing the value of <code>userInput</code>. In fact, if I comment out the <code>do while</code> loop, there is 'no problem' with the execution of the code. It assigns a <code>0</code> to <code>userInput</code> and goes about its business.</p>
  
  <pre><code>#import &lt;Foundation/Foundation.h&gt;
  
  int main (int argc, const char * argv[]) {
      NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  
      float userInput;
      float result;
  
      NSLog(@"3X^3 -5x^2 + 6");
  
      do {
          NSLog(@"What is x?");
          scanf("%f", &amp;userInput);
          NSLog(@"userInput = %f", userInput);
      } while(userInput == 0);
  
      result = 3 * (userInput * userInput * userInput) - 5 * (userInput * userInput) + 6;
      NSLog(@"the result is: %f", result);
  
      [pool drain];
      return 0;
  }
  </code></pre>