这里需要一点帮助。有点背景故事,我开始编码已经 1 个月了。从 C 语言开始,现在我知道结构和数组。下周会做指点。所以,这已经不在了,现在是问题。在这里,我将链接我的源代码、我想要的输出以及程序给出的输出。
这是一条员工记录,您可以在其中添加有限的员工、查看添加的员工、删除他们以及更新他们。使用 C 语言和无指针的基本初学者员工记录。我做的一切都正确,除了最后它搞砸了。你可以从我得到的输出中看到与我想要的输出有点不同。
删除员工后,它会被删除,但是当我在删除旧员工后添加新员工时,当我想查看那里有多少员工时,它不会显示。您可以从我提供的 2 个输出中看到,当我打印出已保存的员工时,除了最后一个输出外,其他一切都相同。我在这里没有发现我的源代码有什么问题。
是否可以帮我解释一下我遗漏的代码行,这将使我的代码工作并打印我想要的输出?
我们正在为我们的 C 语言使用 Visual Studio 2017(我的学院推荐)。 并且需要通过pUTTY(Matrix Server)提交输出。
我的源代码 - 可以从这里给出的链接中找到 https://pastebin.com/spLhtrt5并且这里也给出了源代码
#include <stdio.h>
#define SIZE 4
struct Emp {
int id;
int age;
double sal;
};
int main (void) {
int i, option = 0, counter = 0, new = 0, srch, don = 0;
struct Emp emp[SIZE] = { {0} };
printf ("---=== EMPLOYEE DATA ===---\n\n");
do {
printf ("1. Display Employee Information\n");
printf ("2. Add Employee\n");
printf ("3. Update Employee Salary\n");
printf ("4. Remove Employee\n");
printf ("0. Exit\n\n");
printf ("Please select from the above options: ");
scanf ("%d", &option);
printf ("\n");
switch (option) {
case 0: // Exit the program
break;
case 1: // Display Employee Data
// @IN-LAB
printf ("EMP ID EMP AGE EMP SALARY\n");
printf ("====== ======= ==========\n");
// Use "%6d%9d%11.2lf" formatting in a
// printf statement to display
// employee id, age and salary of
// all employees using a loop construct
// The loop construct will be run for SIZE times
// and will only display Employee data
// where the EmployeeID is > 0
for (i = 0; i < SIZE; i++) {
if (emp[i].id > 0 && emp[i].age > 0 && emp[i].sal > 0) {
printf ("%6d%9d%11.2lf\n", emp[i].id, emp[i].age,
emp[i].sal);
}
}
printf ("\n");
break;
case 2: // Adding Employee
// @IN-LAB
printf ("Adding Employee\n");
printf ("===============\n");
if (counter < SIZE) {
printf ("Enter Employee ID: ");
scanf ("%d", &emp[new].id);
printf ("Enter Employee Age: ");
scanf ("%d", &emp[new].age);
printf ("Enter Employee Salary: ");
scanf ("%lf", &emp[new].sal);
new++;
counter++;
printf ("\n");
break;
} else {
printf ("ERROR!!! Maximum Number of Employees Reached\n\n");
}
break;
case 3:
don = 0;
printf ("Update Employee Salary\n");
printf ("======================\n");
do {
printf ("Enter Employee ID: ");
scanf ("%d", &srch);
for (i = 0; i < SIZE; i++) {
if (emp[i].id == srch) {
printf ("The current salary is %.2lf\n", emp[i].sal);
printf ("Enter Employee New Salary: ");
scanf ("%lf", &emp[i].sal);
don = 1;
printf ("\n");
}
}
if (don == 0) {
printf ("*** ERROR: Employee ID not found! ***\n");
}
} while (don != 1);
break;
case 4:
don = 0;
printf ("Remove Employee\n");
printf ("===============\n");
do {
printf ("Enter Employee ID: ");
scanf ("%d", &srch);
for (i = 0; i < SIZE; i++) {
if (emp[i].id == srch) {
printf ("Employee %d will be removed", emp[i].id);
emp[i].id = 0;
emp[i].age = 0;
emp[i].sal = 0;
counter--;
don = 1;
printf ("\n\n");
}
}
if (don == 0) {
printf ("*** ERROR: Employee ID not found! ***\n");
}
} while (don != 1);
break;
default:
printf ("ERROR: Incorrect Option: Try Again\n\n");
}
} while (option != 0);
printf ("Exiting Employee Data Program. Good Bye!!!\n");
return 0;
}
我想要的输出 - https://pastebin.com/CcrXibB4 .
我得到的输出 - https://pastebin.com/mmvjBVL9 .
最佳答案
您正在将新员工插入索引 new
的位置。当您在插入 4 名员工后删除时,新的仍然指向数组外的 4,因此您的新员工将被写在数组外并且永远不会显示,这也会破坏您的堆栈。
可能的解决方案:在添加新员工时扫描数组以找到空闲位置(例如,如果我理解你的逻辑正确,则找到 id == 0 的员工)并将其位置用作插入索引。
对于您的代码,我还有一些一般性建议:
- 编写小函数,在您的情况下创建一个用于插入的函数,一个用于删除的函数等。
- 单独测试每个功能,因此您无需手动测试所有内容。
- 然后编写测试以查看函数之间如何交互
- 仅在最后添加用户界面(您的菜单、开关盒等)。
通常这可以加快调试速度,如果不是这种情况,至少您可以在此处发布一个更小的示例。
关于c - 删除员工后,然后添加新员工。列出所有保存的员工时,新员工不会出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56614470/