c - Getting user input doesn't work correctly -
i writing simple program in c using structs. user needs enter values struct - name , age. after enter data first time, second time program skips 1 of fields , wants me enter second field of data. can't figure out what's wrong.
struct person { char name[20]; int age; }; void main(){ struct person parray[10]; (int = 0; < 10; i++) { printf("please enter name , age:\n"); printf("name: "); fgets(parray[i].name, 20, stdin); printf("age: "); scanf("%d", &parray[i].age); } } as can see, after entering jonathan , 45 first time, second time skipped name , wants age. why happening? 
i try not mix formatted , unformatted input (e.g., fgets , scanf). here program using fgets input:
#include <stdio.h> struct person { char name[20]; int age; }; int main(){ struct person parray[10]; char numberbuffer[20]; (int = 0; < 10; i++) { printf("please enter name , age:\n"); printf("name: "); fgets(parray[i].name, 20, stdin); printf("age: "); fgets(numberbuffer, 20, stdin); sscanf(numberbuffer, "%d", &parray[i].age); } }
Comments
Post a Comment