delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1994/09/01/12:13:29

Date: Thu, 1 Sep 1994 11:39:53 +0500
From: ronis AT onsager DOT chem DOT mcgill DOT ca (David Ronis)
To: pjones AT acs DOT bu DOT edu
Subject: Re: bug in scanf()
Cc: djgpp AT sun DOT soe DOT clarkson DOT edu

----- Begin Included Message -----

>From pjones AT acs DOT bu DOT edu Thu Sep  1 11:24 EDT 1994
Thanks for the quick reply.  However I don't agree.  You said:


	This is the correct behavior of scanf();  it will work that way on all
	C compilers.
	
This is not the case with SUN's C compiler (at least the one that came
with SunOS 4.3.2), nor is it the case with Microsoft C 6.00A.


	The problem with your test program is that when scanf() encounters an
	illegal character according to its format specification, it will not
	remove it from the input stream, so you are continually trying to scan
	the same invalid string.  One way of fixing this would be to call
	"fflush(stdin)" after when you scanf() has failed, or before every
	scanf() call, whichever is more appropriate...

This is a good suggestion, especially since it won't break my code on
machines where scanf() behaves in the other way.  Unfortunately, it
doesn't work.  Modifying my example as you suggested (see below) makes
no difference.  

Thanks again


David Ronis



#include <stdio.h>

main()
{
	while(1)
	{
		int i,j;

		j=scanf("%d",&i);
		printf("i=%d; j=%d\n",i,j);
		if(j!=1)fflush(stdin);		/* YOUR SUGGESTION */
	}
}


- Raw text -


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