Mail Archives: djgpp/1998/11/16/14:10:45
Eli Zaretskii wrote:
> On Thu, 12 Nov 1998, G.B.Rotman wrote:
>
> > A *portable* way of making sure the newline is not left in the buffer
> > is to force `scanf' to consume it as well, like this:
> >
> > scanf ("%d\n", &x);
> >
> > This doesn't seem to work, put a line like this
> > one in and scanf waits until it has been given
> > two integers but it still leaves the newline in the
> > buffer.
>
> It works for me. I actually tried that line before replying the other
> day. So maybe the actual program you are trying is a bit different,
> and needs somewhat different format.
>
> > Isn't there a proper way to empty the buffer?
>
> No, not in a portable way. And I don't think this is even required in
> this case, anyway.
This simple test program doesn't even work for me,
when I run it scanf waits until two integers are
given and then skips gets, I assume because of the
newline which is still left in the buffer. Or perhaps
scanf() does take the first newline, but not the second
(after I have given it again an integer).
#include <stdio.h>
int main()
{
int a;
char string[25];
puts("\ngive input for scanf()\n");
scanf("%d\n", &a);
puts("give input for gets()\n");
gets(string);
puts("end");
}
- Raw text -