Path: utzoo!utgpu!jarvis.csri.toronto.edu!rutgers!cs.utexas.edu!uunet!mcvax!kth!sunic!dkuug!freja!njk From: njk@freja.diku.dk (Niels J|rgen Kruse) Newsgroups: comp.lang.c Subject: Re^2: scanf(..) Message-ID: <4734@freja.diku.dk> Date: 12 Jun 89 19:33:11 GMT References: <225800176@uxe.cso.uiuc.edu> <11831@bloom-beacon.MIT.EDU> Organization: DIKU, U of Copenhagen, DK Lines: 23 scs@adam.pika.mit.edu (Steve Summit) writes: >Scanf's most miserable problem is that it doesn't discard >unrecognized input. (This is a documented feature, so don't try >getting it changed. It is only a miserable problem when scanf >is being used for interactive user input, which is what everybody >uses it for.) The 'a' sits there in the input buffer, causing >each scanf to fail, and the correct digits you probably typed >after the 'a' are never seen. But if you want scanf to skip bad input, you can just tell it to do so. In this case you could write ... scanf ("%*[^0123456789] %d",&d) ... which will cause scanf to discard leading junk and then read a number. A skip-leading-junk conversion modifier would be nice though. Perhaps a '!' could be used for that : scanf ("%!d",&d) . -- Niels J|rgen Kruse Email njk@diku.dk Mail Tustrupvej 7, 2 tv, 2720 Vanlose, Denmark