title: C: scanf input single character and validation
body: <p>I've encountered a problem when validating a single-char scanf input in C and I cannot find an existing solution that works...</p>
  
  <p>The scenario is: a method is taking a single letter 'char' type input and then validating this input, if the criteria is not met, then pops an error message and re-enter, otherwise return this character value. </p>
  
  <p>my code is:</p>
  
  <pre><code>char GetStuff(void)
  {
      char c;
      scanf("%c", &amp;c);
      while(c != 'A' || c != 'P')
      {
            printf("invalid input, enter again (A for AM or P for PM): ");
            scanf ("%c", &amp;dtChar);
      }
      return c;
  }
  </code></pre>
  
  <p>however, i got the infinite loop of error message no matter what input I type in. I read some other posts and guess it's the problem that %c specifier does no automatically get rid of the newline when I hit enter, and so far I have tried:</p>
  
  <ol>
  <li><p>putting a white space before/after %c like:</p>
  
  <pre><code>scanf(" %c", &amp;c);
  </code></pre></li>
  <li><p>write a separate method or include in this <code>GetStuff</code> method to clean the newline like:</p>
  
  <pre><code>void cleanBuffer(){
    int n;
    while((n = getchar()) != EOF &amp;&amp; n != '\n' );
  }
  </code></pre></li>
  </ol>
  
  <p>Can anyone help me with this problem please? Thank you in advance.</p>