I'm writing a program that stores employee information which includes their first name, last name, and 3-digit id number. The program is supposed to go through the list then display the information for the employee with the lowest id number. Currently I can input the employee information without an issue, but the program can't handle finding the correct employee and displaying it.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct employee
{char first_name[20];
char last_name[20];
int id;
struct employee *next;
}emp;
emp* insertit(emp *first, char firstn[], char last[], int empid);
int main(void)
{
emp *head,
*p;
char empf[20],
empl[20];
int emplid,
j,
no_emps,
k,
l;
char ch,
junk,
nl;
head=NULL;
printf("Enter the number of employees: ");
scanf("%d", &no_emps);
scanf("%c", &junk);
for(j=0;j<20;j++)
empf[j]=' ';
for(k=0;k<20;k++)
empl[k]=' ';
for(l=0;l<no_emps ;l++)
{printf("Enter first name of employee:");
gets(empf);
printf("Enter last name of employee:");
gets(empl);
printf("Enter the 3 digit id number of employee:");
scanf("%d", &emplid);
scanf("%c", &nl);
head=insertit (head, empf, empl, emplid);
for(j=0;j<20;j++)
empf[j]=' ';
for(k=0;k<20;k++)
empl[k]=' ';
}
p=head;
while (p!=NULL)
{printf("The name of the employee is %s %s", p->first_name,
p->last_name);
printf("The id number of the employee is %d\n", p->id);
p=p->next;
}
printf("Hit any character to continue");
scanf("%c", &ch);
}
emp* insertit (emp *first, char firstn[], char last[], int empid)
{
emp *p, *q, *newp;
int found, len, i, id;
found=0;
q=first;
p=first;
while ((p!=NULL) && (!found))
{if (p->id<id)
{q=p;
p=p->next;
}
else
found=1;
}
newp=(emp*) malloc(sizeof (emp));
newp->id=empid;
strncpy(newp->first_name, first, 20);
strncpy(newp->last_name, last, 20);
newp->next=p;
if(q!=p)
q->next=newp;
else
first=newp;
return (first);
}
Where's the issue?