I'm running into some problems in saving a string - a name, for example - into a struct field. I've used gets()
and fgets()
both, but fgets()
isn't working properly either.
I never get the chance to input the first employee name; it skips straight to the employee code and then skips the address too. For some reason, when inputting the second employee, I get to input both the name and code, and then it skips the address again.
Anyone know what I'm doing wrong?
#include <stdio.h>
typedef struct {
char name[150];
int code;
char add[300];
} tEmployee;
int main()
{
printf("How many employees would you like to register?\n");
int n;
scanf("%i", &n);
tEmployee employee[n];
for (int i = 0; i < n; i++)
{
printf("Name: ");
gets(employee[i].name);
printf("Code: ");
scanf("%i", &employee[i].code);
printf("Address: ");
gets(employee[i].add);
printf("%s\n", employee[i].name);
printf("%i\n", employee[i].code);
printf("%s\n", employee[i].add);
}
return 0;
}