So below is my program that contain several function about information of employees that were stored within a structure such as display all of them, search by entering ID or date joined and also edit or add new record. So my problem now is after i use the addRecord function within my program, then it navigated back to the menu, when i choose to displayAll, the new employee's information that i have just entered through the addRecord function is all gone, can someone please help me to spot where my mistakes are? Appreciate it a lot.
#include <stdio.h>
#include <string.h>
#define MAX_SIZE 20
struct Date {
int day;
int month;
int year;
};
struct Employee {
char employeeId[4];
char name[20];
struct Date date;
char department[15];
};
void displayAll(struct Employee em[MAX_SIZE], int employee);
void search(struct Employee em[MAX_SIZE], int employee);
void viewRecord(struct Employee em[MAX_SIZE], int employee);
void editRecord(struct Employee em[MAX_SIZE], int employee);
addRecord(struct Employee em[MAX_SIZE], int employee);
int main()
{
int nEmployees = 5;
int menuChoice;
struct Employee emp[MAX_SIZE] = { { "E01", "Alice Chan", {5, 12, 2008}, "R&D"},
{ "E02", "John", {9, 12, 2011}, "IT"},
{ "E03", "Vivian", {3, 3, 2015}, "HR"},
{ "E04", "Alice Chin", {4, 4, 2011}, "IT"},
{ "E05", "Vivien Tan", {5, 3, 2015}, "HR"}
};
do {
printf("\n\n\nHello human, what do you want to do? \n");
printf("1 for display all employee's information. \n");
printf("2 for search based on date joined. \n");
printf("3 for search based on ID. \n");
printf("4 for editing employees' info. \n");
printf("5 for adding new employee's info. \n");
printf("0 for exit. \n");
printf("> ");
scanf("%d", &menuChoice);
if (menuChoice == 1) {
displayAll(emp, nEmployees);
}
else if (menuChoice == 2) {
search(emp, nEmployees);
}
else if (menuChoice == 3) {
viewRecord(emp, nEmployees);
}
else if (menuChoice == 4) {
editRecord(emp, nEmployees);
}
else if (menuChoice == 5) {
addRecord(emp, nEmployees);
}
else {
printf("The choice is invalid :(. Please try again. ");
}
} while(menuChoice != 0);
}
void displayAll(struct Employee em[MAX_SIZE], int employee) {
int counter = 0;
printf("Employee Details : \n");
printf("-------------------\n");
printf("Employee ID Name Date Joined Department\n");
do {
printf("%-15s%-15s%-2d%-3d%-10d%-s\n", em[counter].employeeId, em[counter].name, em[counter].date.day, em[counter].date.month, em[counter].date.year, em[counter].department);
counter++;
} while(counter < employee);
}
void search(struct Employee em[MAX_SIZE], int employee) {
int counter = 0;
int search1, search2;
printf("Enter the month and year to search (exp : 12 2008): ");
scanf("%d %d", &search1, &search2);
printf("Employee Details : \n");
printf("-------------------\n");
printf("Employee ID Name Date Joined Department\n");
do {
if(search1 == em[counter].date.month && search2 == em[counter].date.year) {
printf("%-15s%-15s%-2d%-3d%-10d%-s\n", em[counter].employeeId, em[counter].name, em[counter].date.day, em[counter].date.month, em[counter].date.year, em[counter].department);
}
counter++;
} while(counter < employee);
}
void viewRecord(struct Employee em[MAX_SIZE], int employee) {
int counter = 0;
char search3[10];
printf("Enter the ID to search : ");
scanf(" %s", &search3);
do {
if(strcmp(search3, em[counter].employeeId) == 0) {
printf("Employee ID > %s\n", em[counter].employeeId);
printf("Name > %s\n", em[counter].name);
printf("Date Joined > %d-%d-%d\n", em[counter].date.day, em[counter].date.month, em[counter].date.year);
printf("Department > %s\n", em[counter].department);
}
counter++;
} while(counter < employee);
}
void editRecord(struct Employee em[MAX_SIZE], int employee) {
int counter = 0;
int editChoice;
char choice[10];
printf("Enter the ID you want to edit > ");
scanf("%s", &choice);
do {
if (strcmp(choice, em[counter].employeeId) == 0) {
printf("What do you want to edit ? ( 1 for name, 2 for date joined, 3 for department) > ");
scanf("%d", &editChoice);
if (editChoice == 1) {
printf("Enter new name > ");
scanf(" %[^\n]", &em[counter].name);
}
else if (editChoice == 2) {
printf("Enter new date joined > ");
scanf(" %d %d %d", &em[counter].date.day, &em[counter].date.month, &em[counter].date.year);
}
else if (editChoice == 3) {
printf("Enter the department > ");
scanf(" %[^\n]", &em[counter].department);
}
printf("Employee ID > %s\n", em[counter].employeeId);
printf("Name > %s\n", em[counter].name);
printf("Date Joined > %d-%d-%d\n", em[counter].date.day, em[counter].date.month, em[counter].date.year);
printf("Department > %s\n", em[counter].department);
}
counter++;
} while(counter < employee);
}
addRecord(struct Employee em[MAX_SIZE], int employee) {
printf("Enter the ID > ");
scanf(" %[^\n]", &em[employee].employeeId);
printf("Enter the name > ");
scanf(" %[^\n]", &em[employee].name);
printf("Enter the date joined (exp: 28 8 2002) > ");
scanf(" %d %d %d", &em[employee].date.day, &em[employee].date.month, &em[employee].date.year);
printf("Enter the department > ");
scanf(" %s", &em[employee].department);
employee++;
return employee;
}
You're never incrementing the nEmployees
variable. Presumably you meant to do something like
nEmployees = addRecord(emp, nEmployees);
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments