scanf() skipped 
Author Message
 scanf() skipped

I have a program that uses scanf() to get some inputs interactively.

the first several scanf()'s ran fine, the program went on to process
some of the inputs, printed out something to the standard output, then
scanf()'s were called again to get some more inputs.  but this time, the
cursor didn't wait for the user to enter data any more, instead, the
program just blasted thru all the new scanf()'s.

what went wrong?  standard input was never closed.  I encountered
the same kind of problems before in college, but really can't remember
what the problem was and its fix.

Thanks a bunch for your help.  Please e-mail me.

Ming



Tue, 27 Mar 2001 03:00:00 GMT  
 scanf() skipped

   I have a program that uses scanf() to get some inputs interactively.

   the first several scanf()'s ran fine, the program went on to process
   some of the inputs, printed out something to the standard output, then
   scanf()'s were called again to get some more inputs.  but this time, the
   cursor didn't wait for the user to enter data any more, instead, the
   program just blasted thru all the new scanf()'s.

This is generally an indication that one of the scanf()'s failed
somewhere in the middle.  scanf() does not skip invalid input, thus it
can become `stuck' at a problem spot.
--
"You call this a *C* question? What the hell are you smoking?" --Kaz

Please: do not email me copies of your posts to comp.lang.c
        do not ask me C questions via email; post them instead



Wed, 28 Mar 2001 03:00:00 GMT  
 scanf() skipped

Quote:

> the first several scanf()'s ran fine, the program went on to process
> some of the inputs, printed out something to the standard output, then
> scanf()'s were called again to get some more inputs.  but this time, the
> cursor didn't wait for the user to enter data any more, instead, the
> program just blasted thru all the new scanf()'s.

Hi Ming Pan,

This is one of the very typical problems of using "scanf()". There
are more. That's why it is generally not a very good idea to use
"scanf()" in anything but the most simple training excercises.

What happens is that "scanf()" finds some input that does not match
the formatting string. It will indicate this in it's return value
(you did not check that, right?) and return immediately. The offending
character(s) will *remain* in the input stream. The next call to
"scanf()" will find them again and will very likely behave similarely.
Especially in loops the result is desasterous.

As I indicated above, the best solution is not to use "scanf()" at
all. Always use "fgets()" for interactive user input. This reads
exactly one line of input. You can process it safely before reading
the next line.

Other methods of processing input are best deligated to compiler
specific input processing function, that are outside the scope
of this newsgroup.

Stephan
(initiator of the campaign against grumpiness in c.l.c)



Fri, 30 Mar 2001 03:00:00 GMT  
 scanf() skipped
Groovy hepcat Ming Pan was jivin' on Fri, 9 Oct 1998 17:30:28 -0500 in
comp.lang.c.
scanf() skipped's a cool scene! Dig it!

Quote:
>I have a program that uses scanf() to get some inputs interactively.

  Is this another joke post or a troll? (It's hard to tell sometimes.)
If it is, then just ignore my explanation and slight rant. If not, pay
attention.

Quote:
>the first several scanf()'s ran fine, the program went on to process
>some of the inputs, printed out something to the standard output, then
>scanf()'s were called again to get some more inputs.  but this time, the
>cursor didn't wait for the user to enter data any more, instead, the
>program just blasted thru all the new scanf()'s.

  If you had the manners to read the group for a while, and read the
FAQ, - before posting - then you would know that scanf() is a bad
function. Its use is frought with toils and troubles.

Quote:
>what went wrong?  standard input was never closed.  I encountered

  scanf() will leave a newline in the input stream, thus skipping
input on the next call since it cannot read past the newline.

Quote:
>the same kind of problems before in college, but really can't remember
>what the problem was and its fix.

  Don't use scanf(). Do read the FAQ. Do read this group for a while
before posting a question, in case someone else has already asked it.
Do use fgets() to read a line of input (including the newline), then
scan with sscanf(). Do read the FAQ. Do read this group for a while
before posting a question, in case someone else has already asked it.
Do read the FAQ. Do read this group for a while before posting a
question, in case someone else has already asked it. (Yes, I do know
I've repeated myself. I did that deliberately, so you'd get the
picture in no uncertain terms.)
--

----- Dig the EVEN NEWER, MORE IMPROVED news sig!! -----

-------------- Shaggy was here! ---------------
    http://aardvark.apana.org.au/~phaywood/
============= Ain't I'm a dawg!! ==============



Tue, 03 Apr 2001 03:00:00 GMT  
 scanf() skipped
Hey, man.  Don't be a{*filter*}.  Dont' be an ass, be a nice fella.  Hey man,
don't be a{*filter*}, dont be an ass.  (Yes, I do know
I've repeated myself. I did that deliberately, so you'd get the picture in
no uncertain terms.)

...

ADK

Quote:
>  Don't use scanf(). Do read the FAQ. Do read this group for a while
>before posting a question, in case someone else has already asked it.
>Do use fgets() to read a line of input (including the newline), then
>scan with sscanf(). Do read the FAQ. Do read this group for a while
>before posting a question, in case someone else has already asked it.
>Do read the FAQ. Do read this group for a while before posting a
>question, in case someone else has already asked it. (Yes, I do know
>I've repeated myself. I did that deliberately, so you'd get the
>picture in no uncertain terms.)
>--

>----- Dig the EVEN NEWER, MORE IMPROVED news sig!! -----

>-------------- Shaggy was here! ---------------
>     http://www.*-*-*.com/ ~phaywood/
>============= Ain't I'm a dawg!! ==============



Tue, 03 Apr 2001 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. skipped printf/scanf ???

2. scanf or n/scanf

3. fscanf: problem skipping characters

4. How to let C# program skip the error code, continue running

5. skip line

6. C# Compiler Skipping Blocks of Code and Methods

7. file processing,skipping around

8. Why: Loop with printf() skips getchar() ?

9. Block memory copy with skip.

10. Q: Why does my code skip gets()

11. fscanf: problem skipping characters

12. Skip first line

 

 
Powered by phpBB® Forum Software