I need help with this structure loop(?)

The Chuckmeister cee1 at ra.MsState.Edu
Tue May 28 11:32:31 AEST 1991


Ok here goes a biggee problem for me, taht I have been working on for DAYS.

The following code is more or less like a program 'savebook.c' in "C: Step
by Step" by the Waite Group, and I had this same problem as in this code,
so i decided to completely start over and write my own code to try to see
my problem.

Ok, easy enough, I set up my structure to read in the author, title, and
year published of, in this case, 5 books and print them out. Simple.
Well when it does the initial runthru of asking for the info, it does fine.
However, on the first looping and the successive ones, it asks for the
title, however, it does not give room or anything for it, it goes ahead
and asks for the author in the space for the title, and teh year works fine.
Does anyone see an obvious flaw,.. is either scanf or gets or puts or printf
adding an extra linefeed.. thats the only thing I can come up with.

So when It finishes teh 'five' it prints out the author and and date [if I
remember correctly] i have run so many test copies.

I am using Borland C++ 2.0, and have tried it with UNIX C, having deleted
the clrscr() along with #include <conio.h> which I guess is incompatible with
UNIX. Also, I tried to doctor it up trying to catch my error by putting
periods in the field and the series of '\b'.. ANyone know an easier way to
do that?

Ok, most important.. does ANYONE see or know whats going wrong with the code.

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);

value was variablized, or whatever the term as float in the structure:

struct book {
     ...
     float value;
     };

then :

main()
{
	struct book libry[MAXBKS];  /* MAXBKS defined as 10 */
        int count = 0;
        ...

then, inside the loop of asking for, yes, title, author, and in this case value
it shoots that error when it gets to the scanf line.. Sheezz.

I could upload that source if needed, but mainly I am after my first question.
Thanks, any help please send to ---> cee1 at ra.msstate.edu <--- Thanks.

----------------BEGIN-------CUT-HERE------------BEGIN------------

/* Checks to see if Borland C++ has a bug 
 */
#include <stdio.h>
#include <conio.h>
#define MAXTIT 50
#define MAXNUMBOOKS 5

struct books {
	char author[MAXTIT];
	char title[MAXTIT];
	int year;
	};

main()
{
	struct books inventory[MAXNUMBOOKS];
	int count = 0;
	int index;

	clrscr();
	puts("           Welcome to the Library Connection");
	puts("                by Charles Evans, 1991");
	puts("");
	while ( count < MAXNUMBOOKS)
	{
		puts("Please enter the author.");
		printf("[..............................]\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b");
		gets(inventory[count].author);
		puts("Now, please enter the title.");
		printf("[..............................]\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b");
		gets(inventory[count].title);
		puts("Finally, enter the year published.");
		printf("[....]\b\b\b\b\b");
		scanf("%d", &inventory[count].year);
		count++;
	}
	clrscr();
	printf("Inventory:\n");
	for ( index = 0; index < 5; index++)
		printf("%s by %s, %d.\n",inventory[index].title,inventory[index].author,inventory[index].year);
	getch();
	return 0;
}
}



More information about the Comp.lang.c mailing list