L
lovecreatesbeauty
/*
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
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