new line character : scanf and getchar

M

Mars

char c;
int i;

scanf("%d",&i);
c=getchar();

I want to read a integer and a character in 2 lines.
Why the getchar always get the \n character from the last line?????

(Sorry for my newbie question, but where is the FAQ section of the
newsgroup??)
 
J

j

Mars said:
char c;
int i;

scanf("%d",&i);
c=getchar();

I want to read a integer and a character in 2 lines.
Why the getchar always get the \n character from the last line?????

In your case, stdin refers to an interactive device and as such, is line
buffered.
This means that data is transferred when a newline is encountered.

If scanf's conversion succeeds you will have a newline left in your input
stream.
Any subsequent read issued on stdin will encounter this newline if you did
not enter additional data after some integer that corresponds to your
conversion
specifier.

One solution would be,

void consume_stdin(void)
{
while((getchar()) != '\n')
;
}


You should also do some error checking with respect to scanf's return value.
(Sorry for my newbie question, but where is the FAQ section of the
newsgroup??)

http://www.eskimo.com/~scs/C-faq/faq.html
 
S

Sontu

Why don't you just write

int i;
char c;

scanf("%d",&i);
fflush(stdin); //flushes the input stream and gets rid of '\n'
character
c=getchar();

Try it out, it will work.
 
J

j

Sontu said:
Why don't you just write

int i;
char c;

scanf("%d",&i);
fflush(stdin); //flushes the input stream and gets rid of '\n'
character
c=getchar();

Try it out, it will work.

Supposing that there is data in the input stream, where do you propose
that it will be flushed to?

This is actually addressed in the FAQ but I want you to answer my question.
 
R

Richard Bos

Sontu said:
Why don't you just write

Why doesn't _who_ just write that? Leave in some context when you post!
If you must use Google-broken-beta, learn to use it properly.
int i;
char c;

scanf("%d",&i);
fflush(stdin); //flushes the input stream and gets rid of '\n'

Because the behaviour fflush() is undefined for input streams. It only
works for output streams.
Try it out, it will work.

Says you. On your edition of your favourite compiler, it may even do
something. It's not required to do anything sane or safe, though.

Richard
 
C

CBFalconer

Sontu said:
Why don't you just write

int i;
char c;

scanf("%d",&i);
fflush(stdin); //flushes the input stream and gets rid of '\n'
character
c=getchar();

Try it out, it will work.

No it won't (portably). It may assasinate somebody. Please don't
give incorrect advice around here. You should have looked up the
validity of fflush for input devices (which is none).
 
K

Keith Thompson

Mars said:
char c;
int i;

scanf("%d",&i);
c=getchar();

I want to read a integer and a character in 2 lines.
Why the getchar always get the \n character from the last line?????

The getchar() reads the '\n' character because the scanf() didn't read it.
 
O

Old Wolf

j said:
In your case, stdin refers to an interactive device and as
such, is line buffered. This means that data is transferred
when a newline is encountered.

Irrelevent, the same problem occurs on any stream. All you can
deduce from this is that there was a newline character after
the number.
Exactly the same thing would have happened if stdin was coming
from a non-interactive device (eg. a file), so I don't know
how you can say that it did refer to an interactive device.
If scanf's conversion succeeds you will have a newline left in
your input stream.
Any subsequent read issued on stdin will encounter this newline
if you did not enter additional data after some integer that
corresponds to your conversion specifier.

Subsequent reading will always encounter the newline (eventually).
(I think this is what you intended to say, although on the first
reading, it seemed to me that you were saying that if additional
data was entered then the newline would not be encountered).
 
J

j

Old Wolf said:
Irrelevent, the same problem occurs on any stream. All you can
deduce from this is that there was a newline character after
the number.
Exactly the same thing would have happened if stdin was coming
from a non-interactive device (eg. a file), so I don't know
how you can say that it did refer to an interactive device.

Yeah, I'm not sure what I was thinking there.
Subsequent reading will always encounter the newline (eventually).
(I think this is what you intended to say, although on the first
reading, it seemed to me that you were saying that if additional
data was entered then the newline would not be encountered).

I was thinking in terms of what he presented.
He just included one call to getchar after scanf.

Something such as ``10aaaaaa\n''
would have yielded a valid conversion
with a subsequent read producing ``a''.
 
T

T O

Old said:
Irrelevent, the same problem occurs on any stream. All you can
deduce from this is that there was a newline character after
the number.
Exactly the same thing would have happened if stdin was coming
from a non-interactive device (eg. a file), so I don't know
how you can say that it did refer to an interactive device.




Subsequent reading will always encounter the newline (eventually).
(I think this is what you intended to say, although on the first
reading, it seemed to me that you were saying that if additional
data was entered then the newline would not be encountered).

Taking this a step further then, what would be a good but simple method
of reading a single character from stdin, evaluating that character (if
c=="Y") for example, and then flushing all buffers.
Is there a simple way to achieve a yes/no response without requring a CR
from the user, thereby presumably negating this problem of /n.
 
K

Keith Thompson

T O said:
Taking this a step further then, what would be a good but simple
method of reading a single character from stdin, evaluating that
character (if c=="Y") for example, and then flushing all buffers.
Is there a simple way to achieve a yes/no response without requring a
CR from the user, thereby presumably negating this problem of /n.

C FAQ 19.1: "How can I read a single character from the keyboard
without waiting for the RETURN key? How can I stop characters from
being echoed on the screen as they're typed?"

<http://www.eskimo.com/~scs/C-faq/q19.1.html>
 
C

CBFalconer

T said:
.... snip ...

Taking this a step further then, what would be a good but simple
method of reading a single character from stdin, evaluating that
character (if c=="Y") for example, and then flushing all buffers.

You could call the following function:

/* Returns EOF for file ended, else '\n' */
int flushln(FILE *f)
{
int ch;

while ((EOF != (ch = getc(f))) && ('\n' != ch)) continue;
return ch;
} /* flushln */
Is there a simple way to achieve a yes/no response without requring a CR
from the user, thereby presumably negating this problem of /n.

No. Without input line buffering there would be no input line
editing, which would be a much bigger nuisance than requiring a
<cr>.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top