I am attempting to create a function which reads the personal details of a person and pass the data to the calling function via a structure, then return the number of names returned.
#include <stdio.h>
#include <string.h>
#define MAX 100
int readin(Employee *emp);
int main()
{
Employee emp[MAX];
int size;
size = readin(emp);
return 0;
}
int readin(Employee *emp)
{
char name[40], tel[40], *p;
int id,size=0,i=0;
double sal;
printf("Enter name:\n");
scanf("\n");
fgets(name,40,stdin);
if (p=strchr(name,'\n')) *p = '\0';
if (strcmp("#",name) != 0) {
while (strcmp("#",name) != 0) {
strcpy(emp->name, name);
printf("Enter tel:\n");
fgets(tel,40,stdin);
if (p=strchr(tel,'\n')) *p = '\0';
strcpy(emp->telno, tel);
printf("Enter id:\n");
scanf("%d",id);
emp->id = id;
printf("Enter salary:\n");
scanf("%f",sal);
emp->salary = sal;
size++;
i++;
printf("Enter name:\n");
fgets(name,40,stdin);
}
}
return size;
}
The structure Employee is defined as follows:
typedef struct {
char name[40];
char telno[40];
int id;
double salary;
} Employee;
However, when I execute the program, the first fgets for the name is ignored, and the loop does not stop even when I key in #, which is supposed to indicate termination. Any help would be appreciated.