I have written the following program, but still have a problem understanding the code . why won't this work?

/*This program will organize a linked list of employees
     Written by Elizabeth Preuss
     January 2005
     Adapted from classwork and handouts
     Version 2.0
     language c(target borland)
   */

   #include <stdio.h>

   #include <string.h>

   typedef struct employee
   {char first [21];
    char last [21];
    int id;
    struct employee *next;
   } EMPID;


   EMPID* insertit(EMPID *first,char firstname[], char lastname[], int idnum);

   int main (void)
   {
   EMPID *head,
	 *p;
   char  empfirst[21];
   char  emplast[21];
   int   idnumber,
	 j,
	 k;
   char ch,
   n1;

   head=NULL;
   for(j=0;j<21;j++)
   empfirst[j]=' ';
   for(k=0;k<NO_EMPID;k++)
   {printf("Enter first name of employee\n");
   gets(empfirst);
   printf("Enter the last name of employee\n");
   gets(emplast);
   printf("Enter the employee id number:  ");
   scanf("%d", &idnumber);
   scanf("%c", &n1);
   head=insertit(head,empfirst, emplast, idnumber);
   for(j=0;j<21;j++)
   empfirst[j]=' ';
   }
   p=head;
   while(p!=NULL)
   {printf("The first name of employee is:  %s ",p ->first);
   printf("The last name of employee is:  %s ",p->last);
   printf("The employee id number is:  %d\n", p->idnumber);
   p=p->next;
   }
   printf("Hit any character to continue");
   scanf("%c", &ch);
   }

   EMPID* insertit (EMPID *first, char firstname[], char lastname[], int idnum)
   /* Function to maintain linked structure
      Written by Elizabeth Preuss
      January 2005
      Adapted from classwork and handouts
      version 2.0
      language c (target borland)
   */
   {
   EMPID *p,
	 *q,
	 *newp;
   int found,
       len,
       i;
       found=0;
       q=first;
       p=first;

   while ((p!=NULL) &&(!found))
   {if (p->idnum<idnum)
   {q=p;
   p=p->next;
   }
   else
   found=1;
   }
   newp=(EMPID*) malloc(size of (EMPID));
   newp->first=firstname;
   newp->last=lastname;
   strncpy(newp->first,firstname,21);
   newp->next=p;
   if(q!=p)
   q->next=newp;
   else
   first=newp;
   return(first);
   }

what does it do? and what should it do according to you?
and use code tags, I won't read any code that doesn't use them.

Code tags would help, yes. See the note about code tags at the top of the forum.

But, offhand, here's what I noticed:
1) I don't see you setting the id number of the node
2) You do a strcpy() on firstname but not on lastname. You should strcpy them both.
3) Setting the name pointers to firstname and lastname is a BAD idea; don't do that because then all the entries point to the same first and last name (the last one entered, probably) from the main.

The code is supposed to input employees and sort them by id number, what are code tags?

Hello,

Read the announcement on the C++ help forum page (the page you looked at when you opened this thread.

What you do is have [code] and [/code] at the beginning of your code.


[code]

// This is a comment
// This is another one
[/code]

Feel free to edit your post on the top. Also, space it out for the proper indenting.

Christian.

>why won't this work?
Because it's dreadfully broken. Format the code properly, then read the errors when you try to compile.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.