-
fgets question
Hi all;
If i have the following:
Code:
typedef struct {
char firstname[10];
char surname[10];
int age;
}database;
and i want to take in values for firstname and surname and age like this:
Code:
for(int i=0; i<5;i++)
{
fgets(ptr[i]->firstname, sizeof(ptr[i]->firstname), stdin);
printf("\nEnter the surname");
fgets(ptr[i]->surname, sizeof(ptr[i]->surname), stdin);
printf("Enter the age");
scanf("%d",&ptr[i]->age);
}
There is some code above missing, ie mallocs etc.
The scanf causes the firstname to be skip on the next round of the loop.
What can i use instead of scanf to solve the problem, i know its to with scanf not taking spaces, so firstname takes the space and thats why its blank
Any ideas?
Thanks
-
scanf() doesn't read the newline, so the fgets() reads the newline. Search this forum for "scanf buffer" and you'll find tons of hits.
-
Didnt find much on it, could you post us a link?
-
Didn't find much? I got 20 pages of hits for that argument.
http://cboard.cprogramming.com/c-pro...tml#post881785
-
So for my struct example i have an array for my int so i can use fgets on it?
-
Its ok i solved it, ignore stupid comment by me above
Thanks