This is a linked list. I am trying to input employee names and addresses. This program works so far but I am having two problems that I need help with
1- How can I store the full street string ex: 410 North Ave.
I have tried fgets,sscanf.....but nothing works it only likes non space strings.... Some suggested that I need to parse it or strcpy...but I think there must be another elegant way of doing this.
2- if the user chooses option 3...then I want to erase the employee record that teh user specifies. ex. if the user types Smith for last name...the I want the program to scan the link list for that last name and erase the record...How can I do that??
Thanks
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct employee_link
{
char last[15];
char first[15];
char street[15];
char city[15];
char state[15];
struct employee_link *pnext;
struct employee_link *pprevious;
}EMPLOYEE;
int main(void)
{
struct employee_link *pfirst =NULL;
struct employee_link *pcurrent =NULL;
struct employee_link *plast =NULL;
char temp[15];
int choice=0;
for(;;)
{
printf("\n\n Choose one:");
printf("\n 1. Add employee");
printf("\n 2. Delete employee");
printf("\n 3. Display employee data");
printf("\n 4. Quit\n\n");
printf(" your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1:
if((pcurrent=(EMPLOYEE *)malloc(sizeof(EMPLOYEE)) )==NULL)
{
fprintf(stderr,"link2way: no storage available\n");
exit(1);
}
if (pfirst==NULL)
{
pfirst=pcurrent;
pcurrent->pprevious=NULL;
}
else
{
plast->pnext=pcurrent;
pcurrent->pprevious=plast;
}
printf("\nenter last name: ");
scanf("%s", pcurrent->last);
printf("enter first name: ");
scanf("%s", pcurrent->first);
printf("enter street address: ");
scanf("%s", temp);
strcpy(pcurrent->street,temp);
printf("enter city: ");
scanf("%s", pcurrent->city);
printf("enter state: ");
scanf("%s", pcurrent->state);
plast=pcurrent; /*save address to last entry*/
break;
case 2:
puts("Enter last name to delete: ");
scanf("%s", temp);
break;
case 3:
while(pcurrent != NULL)
{
printf("\n\n%s\n%s\n",pcurrent->last,pcurrent->first);
printf("%s\n%s, %s\n",pcurrent->street,pcurrent->city,pcurrent->state);
plast=pcurrent;
pcurrent=pcurrent->pprevious;
free(plast);
}break;
case 4: free(plast);
exit(1);
break;
}//end of switch
}//end of loop
return 0;
}