Need help about delete and add in struct
Hi, I'm new to C program, so the task is to add maximum of 4 employees data, then remove one, to show 3 of them. then add a new one, and to show all 4 of them. I need help to find out what's wrong wit this code
code:
Code:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
// Define Number of Employees "SIZE" to be 2
// Declare Struct Employee
#define SIZE 4
struct employee {
int age, id;
double salary;
};
/* main program */
int main(void) {
struct employee emp[4] = { 0 };
int option = 0;
int true = 0;
int i = 0;
int n1 = 0;
int n2 = 0;
int n3 = 0;
int true2 = 0;
// Declare a struct Employee array "emp" with SIZE elements
// and initialize all elements to zero
printf("---=== EMPLOYEE DATA ===---\n\n");
do {
// Print the option list
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: ");
// Capture input to option variable
scanf("%d", &option);
printf("\n");
switch (option) {
case 0: // Exit the program
printf("Exiting Employee Data Program. Good Bye!!!");
printf("\n");
break;
case 1: // Display Employee Data
// @IN-LAB
printf("EMP ID EMP AGE EMP SALARY\n");
printf("====== ======= ==========\n");
for (i = 0; i < 4; i++)
{
if (emp[i].id != 0) {
printf("%6d%9d%11.2lf\n", emp[i].id, emp[i].age, emp[i].salary);
}
}
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
break;
case 2: // Adding Employee
// @IN-LAB
printf("Adding Employee\n");
printf("===============\n");
if (n1 < 4) {
for (i = 0; i < 4; i++)
{
if (emp[i].id == 0)
{
printf("Enter Employee ID: ");
scanf("%d", &emp[n1].id);
printf("Enter Employee Age: ");
scanf("%d", &emp[n1].age);
printf("Enter Employee Salary: ");
scanf("%lf", &emp[n1].salary);
printf("%d\n", n1);
i += SIZE;
}
}
n1++;
}
else {
printf("ERROR!!! Maximum Number of Employees Reached\n");
printf("\n");
}
// Check for limits on the array and add employee
// data accordingly.
break;
case 3:
printf("Update Employee Salary\n");
printf("===============\n");
do {
printf("Enter Employee ID: ");
scanf("%d", &n2);
for (i = 0; i < n1; i++)
{
if (n2 == emp[i].id)
{
true = 1;
printf("The current salary is %.2f\n", emp[i].salary);
printf("\nInput new salary: ");
scanf("%lf", &emp[i].salary);
}
}
if (true == 0)
{
printf("wrong number \n");
}
} while (true == 0);
break;
case 4:
printf("Remove Employee\n");
printf("===============\n");
true2 = 0;
do {
printf("Enter Employee ID: ");
scanf("%d", &n3);
for (i = 0; i < 4; i++) {
if (n3 == emp[i].id)
{
true2 = 1;
printf("Employee %d will be removed", emp[i].id);
emp[i].id = 0;
n1--;
}
}
if (true2 == 0) {
printf("wrong number \n");
}
} while (true2 == 0);
break;
}
if (option > 4)
{
printf("ERROR: Incorrect Option: Try Again\n");
printf("\n");
}
} while (option != 0);
return 0;
}
So it can remove the employee, but once I add the new one, it will only show 3 of the employee data instead of all 4 of them.
Sorry I'm really new to this, and this code I wrote is already with someones help. So please bare with me.