I am working on an assignment where I need to sort a struct alphabetically based upon an employees surname. I have written the program and can run it, but unfortunately it seems to be removing all of the surnames in the struct.
Right now the program reads data from 40 employees contained in a file into a struct.
Am I missing something in the sorting function?
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
typedef struct {
int employee_code;
char *employee_surname[20];
char *employee_forname[20];
int insurable_weeks;
float gross_pay;
float subsidy;
} employee;
employee tempEmployee[100];
void load_data(fp);
void display_menu();
void view_all_employees();
void add_new_employee();
void delete_employee();
void view_qualifying_employees();
void find_disqualified_employees();
void sort_employees_by_subsidy_amount();
void view_total_subsidy_amount();
void sort_employees_by_name();
int main(void)
{
FILE* fp = fopen("data.txt", "r");;
int selection=0, state;
int n=0;
load_data(fp);//open file if it exists. If not, create new file.
fclose(fp);
printf("Please choose an option (1-9)...\n");
do {
display_menu();
//scanf("%d", &selection);
if (state = (scanf("%d", &selection)) != 1) //validates that user is only entering integers.
{
printf("\n\n**** Please only enter numbers. Character strings are not accepted. ****\n\n");
while (getchar() != '\n'); //clears user input to avoid infinite loop.
}
else if (selection < 0 || selection >9)
{
printf("\nInvalid entry. Only enter digits between 1-9,\n");
}
else
{
switch (selection)
{
case 1:
add_new_employee();//view new employees
break;
case 2:
delete_employee();//delete employees
break;
case 3:
view_all_employees();//view all employees and related info
break;
case 4:
view_qualifying_employees();//view employees qualifying for subsidy
break;
case 5:
find_disqualified_employees();//find employees disqualified for subsidy
break;
case 6:
sort_employees_by_name();//sort employee by surname
break;
case 7:
sort_employees_by_subsidy_amount();
break;
case 8:
view_total_subsidy_amount();//total subsidy amount for all employees
break;
//more cases added later
}
}
} while (selection != 9);
printf("\nThank you for using the program! Terminating...\n");
return 0;
}
Here is the function that is causing issues:
void sort_employees_by_name()
{
int n = 100, i, j;
char temp[20];
for (i = 0; i <= n; i++)
{
for (j = 0; j <= n - i; j++)
{
if (strcmp(tempEmployee[j].employee_surname, tempEmployee[j + 1].employee_surname) > 0)
{
strcpy(temp, tempEmployee[j].employee_surname);
strcpy(tempEmployee[j].employee_surname, tempEmployee[j + 1].employee_surname);
strcpy(tempEmployee[j + 1].employee_surname, temp);
}
}
}
}
The declaration what you have to store names is an array of pointers to characters
char *employee_surname[20];
char *employee_forname[20];
But you actually need is only an array of characters
char employee_surname[20];
char employee_forname[20];
Regarding the number of records, its better to store the count in some variable or read the file till you see EOF
.
for (i = 0; i <= n; i++)
is wrong, because you are trying to read more than what you have in your file, get the actual value of n
and use for (i = 0; i < n; i++)