getchar() returns the next available key from the keyboard buffer. If the keyboard buffer is empty, then it will wait for you to type something and press <Enter> key. So, if you type "Yes" <Enter> then each time getchar() is called it will return the next key in the sequence, or 4 different keys in that example. And your program must get all the keys from the keyboard buffer before attempting to get more input.
You can accomplish that in a couple ways -- the simplest way is by using fgets() which will get all the keys at one time up to and including the '\n' (same as <Enter>) fgets() will also correctly handle the backspace key.
Code:
// buffer to hold keyboard input
char input[20];
// get all the keys
fgets(input, sizeof(input), stdin);
// convert first character to upper-case
input[0] = toupper(input[0])
// check for 'Y' or 'N'
if(input[0] == 'Y' || input[0] == 'N')
{
// do something
}
or if you want to get the keys one at a time -- code below will not correctly handle the backspace key.
Code:
// buffer to hold keyboard input
char input[20] = {0};
int index = 0, c;
// get all the keys
while(index < (sizeof(input)-1) && (c = getchar()) != '\n')
input[index++] = c;
// convert first character to upper-case
input[0] = toupper(input[0])
// check for 'Y' or 'N'
if(input[0] == 'Y' || input[0] == 'N')
{
// do something
}