O
Old Wolf
^^
{should be !=)
The original was correct; the purpose of the code
is to consume '\n' characters.
^^
{should be !=)
The original was correct; the purpose of the code
is to consume '\n' characters.
can you tell me how to deal with this "a+b problem" by /fgets/ in yourYour response hints to me that you did not use fgets() or fgetc()
properly. The standard C library does not operate on "files", it
operates on "streams". It is entirely possible with C that one
run of a program will have a stream attached to a file and the
next run with the stream attached to a terminal, without any changes
in the code.
The key to using fgets() or fgetc() with the standard input stream
is to supply the FILE* parameter as the name stdin such as
if ( fgets(&mybuffer, sizeof mybuffer, stdin) != NULL ) { ... }
can you tell me how to deal with this "a+b problem" by /fgets/ in your
format?
P.S.
it's not homework.
the a+b problem:
Input:
The input will consist of a series of pairs of integers a and
b,separated by a space, one pair of integers per line.
Output:
For each pair of input integers a and b you should output the sum of a
and b in one line,and with one line of output for each line in input.
Since '\n' != EOF, couldn't you just writeWalter Roberson said:Exactly -- and my posting specifically indicated that as the
intended behaviour.
Since '\n' != EOF, couldn't you just write
while (getchar() == '\n)
continue;
Some days ago, I written a mini program like this:
#include <stdio.h>
int main()
{
char c;
int n;
scanf("%d",&n);
c = getchar();
return 0;
}
I want to assign n and c specific values,for example,give n and c
values of 5 and 'x' in fact when I input 5 then "enter",the program
is finish.I know when I press "Enter", I assigned '\n' to variable c,
But how can I avoid this?
fflush(stdin) just flushes out the "\n" from the buffer.
Default User said:No, it doesn't. It is undefined behavior. Some implementations provide
extensions that do what you say, but many other don't.
How about this one :
int main()
{
char c;
int n;
scanf("%d",&n);
fflush(stdin);
c = getchar();
Keith said:One system that I know of (Solaris) defines the behavior of
fflush(stdin) to discard any buffered input. I don't know of any
implementations on which it 'just flushes out the "\n"'. (Discarding
all input up to the next '\n' is easy enough to do without using
fflush().)
See questins 12.26a and 12.26b in the comp.lang.c FAQ,
<http://www.c-faq.com/>.
One system that I know of (Solaris) defines the behavior of
fflush(stdin) to discard any buffered input. I don't know of any
implementations on which it 'just flushes out the "\n"'. (Discarding
all input up to the next '\n' is easy enough to do without using
fflush().)
See questins 12.26a and 12.26b in the comp.lang.c FAQ,
<http://www.c-faq.com/>.
--
Keith Thompson (The_Other_Keith) (e-mail address removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
BiGYaN said:
Calling a variadic function without a valid prototype in scope invokes
stdin is undefined. If you define it appropriately by #include <stdio.h>
you then run into the problem that the behaviour of fflush is defined
only for streams open for output or update. stdin is neither.
getchar returns int, not char.
To fit so many fundamental errors into such a short program, presumably
without trying to, is truly impressive.
Still fflush() on an input stream invokes the deamons of undefined behavoirBiGYaN said:@ Keith
Actually you interpreted it wrongly. What I wanted to say was that
fflush(stdin) will flush out the whole buffer. But in this case the
buffer had only a "\n". So I wrote "fflush(stdin) just flushes out the
"\n" from the buffer".
I could never imagine writing a program in C without "#include
<stdio.h>".
I just did not write it as I thought it was quite obvious
to add it at the start.
But I'm sorry if it caused some inconvenience
to my not-so-learned friend.
As far as getchar() returning an int. Yes I know it does, but why
bother with that here.
The original question asked for a solution and
I provided it.
It will work fine in this case.
BiGYaN said:How about this one :
int main()
{
char c;
int n;
scanf("%d",&n);
fflush(stdin);
c = getchar();
return 0;
}
fflush(stdin) just flushes out the "\n" from the buffer.
What I wanted to say was that
fflush(stdin) will flush out the whole buffer.
CBFalconer said:But it won't.
Morris said:Harald van Dijk wrote:
| If the behaviour is undefined, any behaviour is allowed, including
| the behaviour BiGYaN described. Saying that because the behaviour
| is undefined, a particular behaviour will not happen, doesn't make
| sense. You can say that the behaviour is non-portable, and perhaps
| (here, almost always) that you should not rely on it, but not that
| it'll not happen.
Huh?
Might I suggest that wise programmers avoid UB?
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.