//first example
void readInWBW()
{
int ch;
while((ch=getchar()) != EOF)
putchar(ch);
}
when I input "qweCTRL+D", the first time Where I input ctrl+z just flush the buffer, and I re-input "ctrl+d" so it's like "qweCTRL+DCTRL+D", then the EOF work, program terminate. result is
$ ./a.out
qweqwe$
//second example
void guess()
{
int guess = 1;
printf("Pick an integer from 1 to 100. I will try to guess ");
printf("it.\nRespond with a y if my guess is right and with");
printf("\nan n if it is wrong.\n");
printf("Uh...is your number %d?\n", guess);
while (getchar() != 'y'){ //<---------------------------
printf("Well, then, is it %d?\n", ++guess); //<----------
sleep(1);
}
printf("I knew I could do it!\n");
}
In this example, I input "qweCTRL+D", it will shows three times "Well, then...", but if I input CTRL+D again, the program will get into infinite loop. reuslt is:
Pick an integer from 1 to 100. I will try to guess it.
Respond with a y if my guess is right and with
an n if it is wrong.
Uh...is your number 1?
qweWell, then, is it 2? //<--------------------------
Well, then, is it 3?
Well, then, is it 4?
Well, then, is it 5?
Well, then, is it 6?
Well, then, is it 7?
Well, then, is it 8?
Well, then, is it 9?
Well, then, is it 10?
Well, then, is it 11?
^C
//third example
void test()
{
char ch;
while((ch = getchar()) != '#')
putchar(ch);
}
I tried to input "qweCTRL+D" like other example, but after flush buffer, "CTRL+D" not response anymore, even if I input "#", it still not terminate. result is:
$ ./a.out
qweqwe
#
#
^C
$
I don't understand why in example2 and example3 have infinite loop and can't terminate the program. can anyone explains it, thank you.