My program crashes under a certain instance/case, it complies and runs fine otherwise.
We were given a text file formatted like so:
12 JackSprat 2 1 65000
13 HumptyDumpty 5 3 30000
17 BoPeep 2 3 30000
20 BoyBlue 3 2 58000
0
we are required to read from file and store into a struct using a linked list. so far my code looks like this:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NAME_LENGTH 20
typedef struct employeeData
{
int EMP_ID;
char* Name;
int Dept;
int Rank;
double Salary;
struct employeeData *next;
}employee;
employee* InitializeList(int EMP_ID, char* Name, int Dept, int Rank, double Salary)
{
employee* ptr = (employee*)(malloc(sizeof(struct employeeData)));
ptr->Name = (char*)malloc(sizeof(char)*NAME_LENGTH);
strcpy(ptr->Name, Name);
ptr->EMP_ID = EMP_ID;
ptr->Dept = Dept;
ptr->Rank = Rank;
ptr->Salary = Salary;
ptr->next = NULL;
return ptr;
}
employee* insert_by_employee(employee* head, employee* ptr)
{
employee* current = NULL;
current = head;
if(current == NULL || strcmp(current->Name, ptr->Name) > 0)
{
ptr->next = current;
return ptr;
}
else
{
while(current->next != NULL && strcmp(current->next->Name, ptr->Name) < 0)
{
current = current->next;
}
}
ptr->next = current->next;
current->next = ptr;
return head;
}
void query(employee *head, int submenu)
{
printf("
Emp name
");
employee* current;
current = head;
while (current != NULL)
{
if(current->Rank == submenu)
{
printf("%s
", current->Name);
current = current->next;
}
current = current->next;
}
return;
}
void printlist(employee* head)
{
employee* current;
current = head;
printf("EMP_ID EMP NAME DEPT RANK SALARY ");
while ( current != NULL)
{
printf("
%d %s %d %d %d
", current->EMP_ID, current->Name, current->Dept, current->Rank, current->Salary);
current = current->next;
}
return;
}
int main(void)
{
FILE* ifp = fopen("empInfo.txt", "r");
int EMP_ID, Dept, Rank, menu_choice = -1, submenu;
double Salary;
char* Name = (char*)malloc(sizeof(char*)*NAME_LENGTH);
employee *head = NULL;
while(!feof(ifp))
{
fscanf(ifp, "%d %s %d %d %d", &EMP_ID, Name, &Dept, &Rank, &Salary);
{
if (EMP_ID == 0)
break;
}
employee* hold = InitializeList(EMP_ID, Name, Dept, Rank, Salary);
head = insert_by_employee(head, hold);
}
while (menu_choice != 0)
{
printf("
Please select an action from the following menu
");
printf("1 to add a new employee
");
printf("2 to delete an employee
");
printf("3 to modify an employee record
");
printf("4 to query employees by rank
");
printf("5 to print all employee information
");
printf("0 (or any other number) to stop
");
scanf("%d", &menu_choice);
if(menu_choice == 1)
{
printf("choice 1
");
menu_choice = -1;
}
if (menu_choice == 2)
{
printf("Choice 2
");
menu_choice = -1;
}
if (menu_choice == 3)
{
printf("Choice 3
");
menu_choice = -1;
}
if (menu_choice == 4)
{
printf("Please provide rank that you would like to query.
");
scanf("%d", &submenu);
query(head, submenu);
menu_choice = -1;
}
if (menu_choice == 5)
{
printlist(head);
menu_choice = -1;
}
}
fclose(ifp);
return 0;
}
I am having trouble with my query function, the function works except when I query rank 1 it will print JackSprats name then the program crashes. No errors from the compiler do not know what else is wrong.
EDIT* SOLUTION*
placed the break within if statement to make sure the loop was not at the last node. once loop hits last node I had loop break.
void query(employee *head, int submenu)
{
printf("
Emp name
");
employee* current;
current = head;
while (current != NULL)
{
if(current->Rank == submenu)
{
printf("%s
", current->Name);
if(current->next == NULL)
{
break;
}
current = current->next;
}
current = current->next;
}
return;
}
See Question&Answers more detail:
os