I prefer to use same type of input throughout my programs. For example, I usually don't mix scanf() and fgets(). Also, I will write a function to handle user input and use that function. The resulting code seems cleaner to me.
Since you are already using fgets(), why not keep using fgets() instead of getchar()? If you provide a big enough char array, in normal use, there shouldn't be any chars left in the input buffer.
Here is one way:
Code:
void menu ()
{
char input[8192]; /* buffer is large enough, unless we have malicious user or user error? */
printf ("In menu(): Enter A, B, or C: ");
fgets (input, sizeof (input), stdin); /* fgets() will grab up to 8191 chars including the newline char from input buffer and will use last char in array for the '\0' char. */
switch (input[0]) /* we only care about 1st char in the string. */
{
/* catch both upper and lower case */
case 'A':
case 'a':
printf ("Case A, a\n");
break;
case 'B':
case 'b':
printf ("Case B, b\n");
break;
case 'C':
case 'c':
printf ("Case C, c\n");
break;
default:
printf ("WTF?\n");
}
}
Note: if you mix getchar() with the above, you may still have to take care of extra chars in the input buffer.