What's the guideline for dealing with unwanted chars in input stream?



/*
When should we worry about the unwanted chars in input stream? Can we
predicate this kind of behavior and prevent it before debugging and
testing? What's the guideline for dealing with it?

As shown below line #21, I should remove the unwanted characters in
input stream there at that time. Do I miss some other possible errors
in i/o which will happen to occur sometimes in other places? And
welcome your kind comments on following the code, thank you.
*/


1 #define STRLEN 200
2
3 int main(int argc, char *argv[])
4 {
5 int ret = 0;
6 char cust[STRLEN] = {'\0'};
7 char dest[STRLEN] = {'\0'};
8 char flight = '\0';
9 char hotel = '\0';
10
11 printf("Customer name: ");
12 gets(cust);
13 printf("Destination: ");
14 gets(dest);
15
16 printf("Will flight be available: ");
17 flight = getchar();
18 printf("Will hotel be available: ");
19
20 /* remove unwanted chars in input stream here now */
21 while(getchar() != '\n'); //intended null loop body
22 hotel = getchar();
23
24 printf("\n- summary -\n");
25 printf("Customer name\t: %s \n" ,cust);
26 printf("Destination\t: %s \n" ,dest);
27 printf("is flight available\t: %c \n", flight);
28 printf("is hotel available\t: %c \n", hotel);
29
30 return ret;
31 }
32

.



Relevant Pages

  • setbuf(stdin,NULL) cant work, why?
    ... why does my program work like buffered? ... int main ... c1 = getchar(); ... If the input stream is unbuffered, where does the 'new line' character ...
    (comp.lang.c)
  • Re: How to prevent the quick disappearance of window
    ... tell me how to fix this problem? ... Add a getchar() just before the return from main. ... characters except the first one on the input stream if the user entered ...
    (comp.lang.c)
  • Re: getchar() problem
    ... But, when I wrote a program using this function, it looks like the reading of the input stream happens only after I press the ... read the characters as soon as possible. ... int main ... while (ch = getchar()> EOF) ...
    (comp.lang.c)
  • Re: getchar() problem
    ... SR> The getchar() function is expected to fetch the next character ... SR> in the input stream and return it. ...
    (comp.lang.c)