Possible Duplicate:
Dev-C++ Input skipped
I am trying to read an array of character strings from stdin using fgets, but the first string I want to read is always ignored. What is causing this issue?
#include <stdio.h>
int main()
{
int i;
struct material
{
char name[30];
float price, kg;
};
unsigned m,nr;
printf("Lorry capacity=");
scanf("%u", &m);
printf("Number of materials=");
putchar('\n');
scanf("%u", &nr);
struct material list[nr];
for (i=0; i<nr; i++)
{
printf("Name=");
fgets(list[i].name, 30, stdin);
}
putchar('\n');
for (i=0; i<nr; i++)
{
printf("%s ", list[i].name);
}
return 0;
}
One of the things that's generally misunderstood is that when you ask for input from the user, you get freebies.
When the computer ask for the amount of materials:
And you enter "51" you're really getting three characters:
'5'
,'1'
, and'\n'
. Everything comes with a newline character when you hit the enter key.Different utilities that get input from the user handle this different.
fgets()
will read the newline and then it stores the input as"51\n"
. If you read it as a string usingscanf()
's string format specificer"%s"
you'll get only the digits"51"
and the newline character is left on thestdin
buffer.So in your case you read a number via
scanf
:Which leaves the newline, then when you try to do the next read:
fgets
picks up that newline and stores it.So there's a bunch of fixes possible here, one is to just use
fgets()
only, another is to consume the newlines withgetchar()
after each scanf, choice is up to you.BTW: You can insert a newline in your printfs: printf("Number of materials=\n");
This is a very common error. After reading a number with scanf, the newline you typed by pressing ENTER is left in the input buffer, so the first call to fgets reads the (very short) line that consists of only that newline.
You can just do a fgets after each scanf, which will then eat the pending newline:
The
scanf("%u", &nr);
leaves the newline in the input buffer, sofgets
finds an empty line without requiring further input to be entered.It is generally a bad idea to mix
(f)scanf
andfgets
, for that reason (among others).As a quick fix, empty the input buffer before the first
fgets
,A more principled fix would be to read in the values before using
fgets
to get an entire line including the newline, and decode the numbers withstrtoul
or maybesscanf
.I noticed that I can get rid of the newline left in the input buffer, by reading it with
getchar()
.Also, I had to use the following code to remove the trailing newline character from fgets() input: