/*
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 16 2716
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)
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/>
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)
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 wrote: "Chuck F. " <cb********@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. sl*******@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. sl*******@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)
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. sl*******@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)
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.
clayne said: Anyways, I think the original poster was looking for ideas on typical methods..
I gave him an excellent idea elsethread.
comp.lang.c spends way too much time on pedantic bullshit vs. getting any thing actually answered
You have drawn an incorrect conclusion based on insufficient data. If you'd
read my first reply in this thread, you'd have seen the following text:
"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)
Mark L Pappin wrote: "Chuck F. " <cb********@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.
Meaning that the loop continues. This sort of confusion is why I
would write the thing as:
while (('\n' != (ch = getchar())) && (EOF != ch)) continue;
--
"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/>
lovecreatesbeauty wrote: Mark L Pappin wrote: "Chuck F. " <cb********@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. sl*******@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.
Sorry, I thought you wrote:
while(getchar() != '\n')
hotel = getchar();
Misread your code, missed the semicolon at the end of the while.
"Chuck F. " <cb********@yahoo.com> writes: Mark L Pappin wrote: "Chuck F. " <cb********@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.
Meaning that the loop continues. This sort of confusion is why I would write the thing as:
while (('\n' != (ch = getchar())) && (EOF != ch)) continue;
When there are two tests, as there are here, it seems better (IMO) to
write the assignment as the first part of a comma expression:
while( ch = getchar(), ch != '\n' && ch != EOF ) ...
Personally I find it easier to read expressions that have less visual
clutter. Also I think it helps to get the assignment right out front
(left out front? fuggedaboudit) where it's immediately obvious that an
assignment is happening.
lovecreatesbeauty wrote: /* 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
Hi,
If you want to read some data character to character, and to do it
directly using C, you are developing a "parser". It is a few long to
explain how to implement it, but these are some elements used in the
most usual implementation:
a) a variable with the last character read.
b) "ungetc" calls to discard the last character if you have read too
much.
c) An integer to mark the state.
If you want more details, do not hesitate to ask.
When the problem starts to be too much complex to code manually this
part, some tools like "lex" and "yacc"(="bison") will do most part of
the work for you.
Other more complex resources are also available. Welcome to the great
world of the grammars.
Kind regards.
"lovecreatesbeauty" <lo***************@gmail.com> wrote Thank you, then do I also need to treat the EOF character specially?
Usually, yes.
You should always write programs with the assumption that any sequence of
input is possible. This includes valid input being suddenly truncated, input
designed by someone with access to your source code deliberately intended to
make the program malfunction, input produced by machines rather than humans,
and so on.
Tim Rentsch wrote: "Chuck F. " <cb********@yahoo.com> writes:
.... snip ... while (('\n' != (ch = getchar())) && (EOF != ch)) continue;
When there are two tests, as there are here, it seems better (IMO) to write the assignment as the first part of a comma expression:
while( ch = getchar(), ch != '\n' && ch != EOF ) ...
Personally I find it easier to read expressions that have less visual clutter. Also I think it helps to get the assignment right out front (left out front? fuggedaboudit) where it's immediately obvious that an assignment is happening.
A further option, avoiding the confusing (to a neophyte) comma
operator, and the evaluated assignment, is:
do {
ch = getchar();
} while (('\n' != ch) && (EOF != ch));
Avoiding all the Cisms that are not found in other languages.
--
"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/>
On 2006-01-01, Richard Heathfield <in*****@invalid.invalid> wrote: 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.
That depends. Some implementations have types of streams for which you
can clearerr() and get more data, even when it was a "genuine" EOF.
For example, on tty devices on UNIX-derived systems, the user types a
particular character once at the beginning of the line [or twice
elsewhere] to trigger end-of-file status. getchar() will continue to
yield EOF until the end-of-file indicator is cleared, but can yield more
data after it has been cleared. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Matthew |
last post by:
I am working on a simple text adventure game. I have implemented one
word commands and a bit for two words. I have a problem though the
movement commands n and s (north and south respectivly) don't...
|
by: Phil Amey |
last post by:
In a web based form I am able to make sure that there is text in an input field but I want to restrict the user from using such characters as ~
# & '
How can I modify this JavaScript below to...
|
by: Generic Usenet Account |
last post by:
I am trying to set up a delimiter character string for the input stream
such that the delimiter string is "skipped over" in the input stream.
Can someone suggest how to do this with some sample...
|
by: herrcho |
last post by:
What's the difference between STDIN and Keyboard buffer ?
when i get char through scanf, i type in some characters and press
enter,
then, where do the characters go ? to STDIN or Keyboard...
|
by: Steve Kobes |
last post by:
I ran the following program on gcc:
#include <stdio.h>
int main(void)
{
unsigned int i;
char c;
if (scanf("%x", &i) == 1)
|
by: David Sworder |
last post by:
Hi,
I'm writing an application in which a client (C#/WinForms) and server
(C#/service) interact with one another. The client establishes a "session"
with the server but for scalability reasons...
|
by: Yobbo |
last post by:
Hi All
I use the following script to stop users typing in anything but standard
chars (eg letters, numbers, etc) into a input textbox:
<!--
// 8 = backspace
// 9 = tab
// 46 = del
// 190 =...
|
by: Gianni Mariani |
last post by:
Do you have a preference on maximum line width for C++ code?
I've seen the craziest debates on this most silly of topic.
I have witnessed engineers spent oodles of time fiddling with line...
|
by: stefcollect |
last post by:
Hi all,
I am pretty new to PHP and am stuck on - what I think - is a generic
string handling problem.
I need to read and manipulate some HTML files and have a problem in
getting some...
|
by: Rina0 |
last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: erikbower65 |
last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps:
1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal.
2. Connect to...
|
by: linyimin |
last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
|
by: erikbower65 |
last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA:
1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
|
by: kcodez |
last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
|
by: DJRhino1175 |
last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this -
If...
|
by: Rina0 |
last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
|
by: DJRhino |
last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer)
If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _
310030356 Or 310030359 Or 310030362 Or...
| |