为什么我的输入在通过函数输入结构后消失了?
Why my input gone after input into a structure through a function?
下面是我的程序,其中包含有关存储在结构中的员工信息的几个功能,例如显示所有这些信息、通过输入 ID 或加入日期进行搜索以及编辑或添加新记录。所以我现在的问题是在我的程序中使用 addRecord 函数后,它导航回菜单,当我选择显示全部时,我刚刚通过 addRecord 函数输入的新员工信息都消失了,有人可以帮忙吗我发现我的错误在哪里?非常感谢。
#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;
}
您永远不会递增 nEmployees
变量。大概你打算做类似
的事情
nEmployees = addRecord(emp, nEmployees);
下面是我的程序,其中包含有关存储在结构中的员工信息的几个功能,例如显示所有这些信息、通过输入 ID 或加入日期进行搜索以及编辑或添加新记录。所以我现在的问题是在我的程序中使用 addRecord 函数后,它导航回菜单,当我选择显示全部时,我刚刚通过 addRecord 函数输入的新员工信息都消失了,有人可以帮忙吗我发现我的错误在哪里?非常感谢。
#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;
}
您永远不会递增 nEmployees
变量。大概你打算做类似
nEmployees = addRecord(emp, nEmployees);