title: How to limit user input to a single char only
body: <p>I have to get user input but only a single character either 1-3. If the user inputs anything else, an error message should return. I have the following, which is a portion of my code:</p>
  
  <pre><code>char val = 'i';
  while (1) {
     if (val == 'i') {
        printf("Enter option: (1/2/3)\n");
        scanf(" %c", &amp;num);
     }
     else if (num == '1') {
        //do things
     }
     else if (num == '2') {
        //do things
     }
     else if (num == '3') {
        //do things
     }
     else {
        // return error
     }
  </code></pre>
  
  <p>My problem is, I'm taking in a <code>%c</code> so a single char, but the user might input something such as <code>123</code> and it would still take in the first char, in this case <code>1</code>. The else statement at the end doesn't take care of all other alternate cases.</p>
  
  <p>Is there a way to limit the user input to a single char between a given range? I've also tried <code>fgets()</code> and <code>strncmp()</code> but had problems with those too.</p>