Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   What's the guideline for dealing with unwanted chars in input stream? (http://www.velocityreviews.com/forums/t440710-whats-the-guideline-for-dealing-with-unwanted-chars-in-input-stream.html)

lovecreatesbeauty 12-31-2005 07:24 AM

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


Richard Heathfield 12-31-2005 10:38 AM

Re: What's the guideline for dealing with unwanted chars in input stream?
 
lovecreatesbeauty said:

> /*
> When should we worry about the unwanted chars in input stream?


When you know they're unwanted.

> Can we
> predicate this kind of behavior and prevent it before debugging and
> testing? What's the guideline for dealing with it?


Decide what you wish to keep and what you wish to discard. Devise an
algorithm for distinguishing between them. Implement the algorithm.

> 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[])


Well done, you got main() right. A good start.

> 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: ");


Undefined behaviour. You forgot to #include <stdio.h>

> 12 gets(cust);


Never, ever, ever, ever, ever, ever, ever use gets(). Use fgets instead, as
it allows you to specify how much storage space you have available for the
input. Any input that won't fit will stay in the stream awaiting collection
by the next function to read from that stream.

> 13 printf("Destination: ");
> 14 gets(dest);


Never, ever, ever, ever, ever, ever, ever use gets(). Use fgets instead, as
it allows you to specify how much storage space you have available for the
input. Any input that won't fit will stay in the stream awaiting collection
by the next function to read from that stream.

> 15
> 16 printf("Will flight be available: ");
> 17 flight = getchar();


Why not use fgets here too?

> 18 printf("Will hotel be available: ");
> 19
> 20 /* remove unwanted chars in input stream here now */
> 21 while(getchar() != '\n'); //intended null loop body


This won't work, because it will keep reading and discarding characters up
to AND INCLUDING the first non-newline, which is presumably supposed to be
data.

I suggest capturing all your data in string form, using fgets (for now, that
is - later you'll probably devise your own input routine when you have a
lot more experience), even if it's single-character data. It's easy enough
to pick a single character out of a string if that's all you need from it.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)

Chuck F. 12-31-2005 01:55 PM

Re: What's the guideline for dealing with unwanted chars in inputstream?
 
Richard Heathfield wrote:
> lovecreatesbeauty said:
>

.... big snip ...
>
>> 19
>> 20 /* remove unwanted chars in input stream here now */
>> 21 while(getchar() != '\n'); //intended null loop body

>
> This won't work, because it will keep reading and discarding
> characters up to AND INCLUDING the first non-newline, which is
> presumably supposed to be data.


Hunh? It will remove chars from the stream, including the '\n'.
The fault is that it doesn't check for EOF, which can lead to a
fairly long wait.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>

Richard Heathfield 12-31-2005 02:28 PM

Re: What's the guideline for dealing with unwanted chars in input stream?
 
Chuck F. said:

> Richard Heathfield wrote:
>> lovecreatesbeauty said:
>>

> ... big snip ...
> >
>>> 19
>>> 20 /* remove unwanted chars in input stream here now */
>>> 21 while(getchar() != '\n'); //intended null loop body

>>
>> This won't work, because it will keep reading and discarding
>> characters up to AND INCLUDING the first non-newline, which is
>> presumably supposed to be data.

>
> Hunh? It will remove chars from the stream, including the '\n'.


Oops. My apologies. I read it as == for some reason.


--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)

slebetman@yahoo.com 12-31-2005 09:42 PM

Re: What's the guideline for dealing with unwanted chars in input stream?
 
Richard Heathfield wrote:
> Chuck F. said:
>
> > Richard Heathfield wrote:
> >> lovecreatesbeauty said:
> >>

> > ... big snip ...
> > >
> >>> 19
> >>> 20 /* remove unwanted chars in input stream here now */
> >>> 21 while(getchar() != '\n'); //intended null loop body
> >>
> >> This won't work, because it will keep reading and discarding
> >> characters up to AND INCLUDING the first non-newline, which is
> >> presumably supposed to be data.

> >
> > Hunh? It will remove chars from the stream, including the '\n'.

>
> Oops. My apologies. I read it as == for some reason.
>


More accurately, the "while(getchar() != '\n');" part removes every
other character in the stream so that the following "getchar()" will
only get 'even' characters.

If "Hello world\n" is in the stream then you would only be getting "el
ol\n". In this case, because there are an odd number of chars in the
stream before the "\n", the while loop will even miss the newline.


Mark L Pappin 01-01-2006 12:29 AM

Re: What's the guideline for dealing with unwanted chars in input stream?
 
"Chuck F. " <cbfalconer@yahoo.com> writes:

> Richard Heathfield wrote:
> > lovecreatesbeauty said:

> ... big snip ...
> >> 20 /* remove unwanted chars in input stream here now */
> >> 21 while(getchar() != '\n'); //intended null loop body

> > This won't work, because it will keep reading and discarding

>
> > characters up to AND INCLUDING the first non-newline, which is
> > presumably supposed to be data.

>
> Hunh? It will remove chars from the stream, including the '\n'. The
> fault is that it doesn't check for EOF, which can lead to a fairly
> long wait.


Not so long a wait, as (EOF != '\n') != 0.

mlp

lovecreatesbeauty 01-01-2006 03:08 AM

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

Mark L Pappin wrote:
> "Chuck F. " <cbfalconer@yahoo.com> writes:
>
> > Richard Heathfield wrote:
> > > lovecreatesbeauty said:

> > ... big snip ...
> > >> 20 /* remove unwanted chars in input stream here now */
> > >> 21 while(getchar() != '\n'); //intended null loop body
> > > This won't work, because it will keep reading and discarding

> >
> > > characters up to AND INCLUDING the first non-newline, which is
> > > presumably supposed to be data.

> >
> > Hunh? It will remove chars from the stream, including the '\n'. The
> > fault is that it doesn't check for EOF, which can lead to a fairly
> > long wait.

>
> Not so long a wait, as (EOF != '\n') != 0.
>
> mlp


Thank you, then do I also need to treat the EOF character specially?

> 5. slebetman@yahoo.com
> Jan 1, 5:42 am show options
>
>More accurately, the "while(getchar() != '\n');" part removes every
>other character in the stream so that the following "getchar()" will
>only get 'even' characters.


>If "Hello world\n" is in the stream then you would only be getting "el
>ol\n". In this case, because there are an odd number of chars in the
>stream before the "\n", the while loop will even miss the newline.


And I don't understand this reply exactly, sorry.


Richard Heathfield 01-01-2006 05:55 AM

Re: What's the guideline for dealing with unwanted chars in input stream?
 
slebetman@yahoo.com said:

> More accurately, the "while(getchar() != '\n');" part removes every
> other character in the stream so that the following "getchar()" will
> only get 'even' characters.


Nonsense.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)

Richard Heathfield 01-01-2006 05:58 AM

Re: What's the guideline for dealing with unwanted chars in input stream?
 
lovecreatesbeauty said:

> Thank you, then do I also need to treat the EOF character specially?


If getchar() yields EOF, it means you won't be getting any more data from
this stream, no matter how much you try, so if you haven't got enough data
to complete your task you might as well emit an error message and quit.

>
>> 5. slebetman@yahoo.com
>> Jan 1, 5:42 am show options
>>
>>More accurately, the "while(getchar() != '\n');" part removes every
>>other character in the stream so that the following "getchar()" will
>>only get 'even' characters.

>
>>If "Hello world\n" is in the stream then you would only be getting "el
>>ol\n". In this case, because there are an odd number of chars in the
>>stream before the "\n", the while loop will even miss the newline.

>
> And I don't understand this reply exactly, sorry.


It's just nonsense which you can safely ignore.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)

clayne 01-01-2006 08:34 AM

Re: What's the guideline for dealing with unwanted chars in input stream?
 
Anyways, I think the original poster was looking for ideas on typical
methods.. comp.lang.c spends way too much time on pedantic bullshit vs.
getting any thing actually answered as originally asked in preference
for focusing on the absence of <stdio.h>.

To the original poster:

switch on the return from getchar() and case on values for which you
actually care about, with a default case either doing what would be
intuitive or re-issuing the output query to the user.



All times are GMT. The time now is 11:29 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.