www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/11/16/14:10:45

From: "G.B.Rotman" <g DOT b DOT rotman AT stud DOT biol DOT ruu DOT nl>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: scanf/gets bug?
Date: Mon, 16 Nov 1998 19:20:55 +0100
Organization: Academic Computer Centre Utrecht, (ACCU)
Lines: 47
Message-ID: <36506D06.965F8EC6@stud.biol.ruu.nl>
References: <Pine DOT SUN DOT 3 DOT 91 DOT 981115130505 DOT 1381E-100000 AT is>
NNTP-Posting-Host: warande3077.warande.uu.nl
Mime-Version: 1.0
X-Mailer: Mozilla 4.5 [en] (Win98; I)
X-Accept-Language: en
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com


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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019