Mail Archives: djgpp/1999/04/05/17:26:31
You have two problems.
1. fflush(stdin) does absolutely nothing in DJGPP (nor should it).
Using gets() is the right thing to do there.
2. The *first* scanf doesn't read the whole line - it leaves a bit
behind, which is read as the first line in your loop. I recommend
doing something like this:
char buf[100];
fgets(buf, 100, stdin);
sscanf(buf, "%d", &z);
Don't use gets because it has no way of stopping you from overflowing
your buffer.
> >> ...
> >> scanf ("%d", &z);
> >>
> >> printf ("Text:\n");
> >>
> >> for (i=0;i<z;++i)
> >>
> >> {
> >>
> >> fflush(stdin); /* i quess here's your problem */
> >>
> >> gets(text[i]);
> >>
> >> l[i]=strlen(text[i]);
> >>
> >> }
> >> ...
> >> When I compile it with DJGPP and it should read n(z=n) Lines it only
> >> reads n-1 lines. But when I compile it
> >> with TurboC it runs correctly. Does someone know where there is a bug?
> >
> > As i recall, you can't fflush _stdin_ stream, only _stdout_ stream. This
> > may cause your problem and irregularities between those two compilers you
> > used.
> >
> No, that can't be the problem, when I leave out the fflush(stdin), it's
> still wrong.
> But when I replace the gets(text[i]) by scanf(text[i]) it runs
> correctly. But with
> scanf you can only read words but no sentences(several words with
> blanks). Can someone help?
- Raw text -