1

I am writing a simple program where it records data for employees. I have a struct array that is initialized to null but it's displaying "0" instead of just blank.

My code:

#define SIZE 4
struct EmployeeData {
     int id;
     int age;
     double salary;
};

struct EmployeeData emp[SIZE] = { { { 0 } } };

[ ... ]

    case 4: // Remove Employee
        printf("Remove Employee\n");
        printf("===============\n");
        printf("Enter Employee ID: ");
        scanf("%d", &j);

        do {
            for (i = 0; i < SIZE; i++) {
                if (j == emp[i].id) {
                    printf("Employee %d will be removed\n", j);
                    emp[i].id = '\0';
                    emp[i].age = '\0';
                    emp[i].salary = '\0';
                    end = 1;
                }
            }
        } while (end != 1);
        break;

When I display the struct array it displays something like:

EMP ID  EMP AGE EMP SALARY
======  ======= ==========
   112       24    8999.99
   113       29    7999.99
   114       88    6999.99
     0        0       0.00

Even when I remove/delete an employee, it would just replace it with 0 instead of leaving it blank. So when I delete a employee, for example id #113, I just want it to be like:

EMP ID  EMP AGE EMP SALARY
======  ======= ==========
   112       24    8999.99
   114       88    6999.99

Instead of:

EMP ID  EMP AGE EMP SALARY
======  ======= ==========
   112       24    8999.99
     0        0       0.00
   114       88    6999.99
     0        0       0.00

I have tried everything I saw online and nothing helped me. Thank you in advance!

4
  • Do you realize by writing emp[i].salary = '\0'; you are assigning these values to 0 and not to "blank"? An int and a double have no "blank" representation in C. Commented Apr 27, 2018 at 5:28
  • 1
    Don't use do / while, just add a break statement when the employee is found, if you can assume there are no duplicate entries. Commented Apr 27, 2018 at 5:50
  • There's no such thing as a blank struct in C so you cannot do that. Instead, you have to decide how to represent absence of an employee at the application level. Commented Apr 27, 2018 at 6:06
  • Please, post a Minimal, Complete and Verifiable example. As you have post, it's impossible to run your code and check, without probably solving the problem in the way. Commented Apr 30, 2018 at 14:55

2 Answers 2

3

You cannot remove an entry from an array: the array size is set to SIZE, so you always have SIZE entries.

You can use a specific value for one of the fields to signify that the entry is invalid or available. The value 0 for id seems appropriate for this as it is the default value for structure and array elements not explicitly specified in the initializer and the value set by you delete code.

Just test this value in your printing loop and skip these records.

The first listing would become:

EMP ID  EMP AGE EMP SALARY
======  ======= ==========
   112       24    8999.99
   113       29    7999.99
   114       88    6999.99

And after deleting the record of employee id #113:

EMP ID  EMP AGE EMP SALARY
======  ======= ==========
   112       24    8999.99
   114       88    6999.99
Sign up to request clarification or add additional context in comments.

Comments

2

Your array is an array of structs. Within each element of your array are three numeric fields that are set to '\0', which is equal to zero. I'm assuming you're using a simple for loop that iterates over all the elements of the array and prints out each field of each element. However, since your fields are int and double, the function you are using (likely something like printf("%d\t%d\t%f", emp[i].id, emp[i].age, emp[i].salary)). Your zeroed out fields are then converted to their character representation, i.e. '0' and printed onto the screen. In order to get the result you want, the simplest way would be to change '\0' to an "illegal value", say -1. You can then check for that value each time you attempt to print an entry. Something like if(emp[i].id == -1) continue; should do the trick.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.