I need help with this structure loop(?)

der Mouse mouse at thunder.mcrcim.mcgill.edu
Sun Jun 2 06:52:24 AEST 1991


In article <789 at ra.MsState.Edu>, cee1 at ra.MsState.Edu (The Chuckmeister) writes:

> [program reading stuff in a loop, first time around works fine,
> second time around skips first question]

> Oh, back to thinking of what the actual code from my C book .. it
> gave the error: 'scanf : floating point formats not linked' and
> Abormal Prog. Term.

> What the heck did that, I used:

> scanf("%f", &libry[count++].value);

The problem here is that the DOS compiler you used tries to be smart
and not bring in the floating-point support unless you use it.
However, you didn't do any floating-point operations the compiler could
see; all the floating-point code was hidden in scanf().

A legacy of machines with tiny memories....

I've stripped out most of this code; only the relevant bits remain.

> 	while ( count < MAXNUMBOOKS)
> 	{
> 		gets(inventory[count].author);
> 		gets(inventory[count].title);
> 		scanf("%d", &inventory[count].year);
> 	}

The problem is that the scanf is *not* reading the newline at the end
of that line.  That remains in the stdio buffer for the gets() at the
beginning of the next loop to trip over.

Generally you don't want to mix line-based I/O (like gets) with
format-based I/O (like scanf).  You should probably replace the scanf
with a call to fgets and then some sort of conversion routine, perhaps
a call to sscanf.  Which leads me into my next point....

You shouldn't be using gets().  gets() should not exist; please pretend
it doesn't.  Those who have to maintain your code will thank you.  Use
fgets() instead.

The reason for this is that it has no checking to prevent overrunning
the buffer passed to it, and by its very design *cannot* have any such
checking.  As soon as the day comes when someone types more input than
your buffer is prepared to handle, your program will fail in strange
and mysterious ways - anything from coming crashing down to giving
sutbly wrong answers.  gets() was the cause of one of the bugs
exploited by the RTM Internet worm of November '88, for precisely the
reasons I just outlined.

					der Mouse

			old: mcgill-vision!mouse
			new: mouse at larry.mcrcim.mcgill.edu



More information about the Comp.lang.c mailing list