Thread: Need help about delete and add in struct

  1. #1
    Registered User
    Join Date
    Oct 2018
    Posts
    6

    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.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You have a 391 line program that could be reduced to less than 180 lines if you removed all that unnecessary whitespace. Generally, you only need one blank line to separate logical sections of code, and maybe sometimes you might use two blank lines to emphasize the separation between the sections, or even three blank lines, but you pretty much never need more than three blank lines anywhere as all that would do is make your code harder to read because readers have more to process vertically than they need. So here's my sensibly reduced version of your code:
    Code:
    #define _CRT_SECURE_NO_WARNINGS
    
    #include <stdio.h>
    
    // Define Number of Employees "SIZE" to be 2
    #define SIZE 4
    
    // Declare Struct Employee
    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;
    }
    A few comments:
    • You defined SIZE to be 4. That's good, so use SIZE instead of hardcoding 4 everywhere.
    • Please avoid naming your variables n1, n2, n3. What on earth do they mean? I'm guessing that you might want to use variable names like employee_count and employee_id instead.
    • What on earth does true and true2 mean? It looks like you are using them as boolean variables to denote whether or not an employee with a given id has been found, but that's very awkward when true can be false (oh, the contradiction!). Rather, use just one variable and name it something like employee_found.
    • Avoid using a do while loop when you can express the logic easily with a while loop. For example, your code to update salary would be easier to understand when written as:
      Code:
      employee_found = 0;
      while (!employee_found) {
          printf("Enter Employee ID: ");
          scanf("%d", &employee_id);
      
          for (i = 0; i < SIZE; i++)
          {
              if (employee_id == emp[i].id)
              {
                  employee_found = 1;
                  printf("The current salary is %.2f\n", emp[i].salary);
                  printf("\nInput new salary: ");
                  scanf("%lf", &emp[i].salary);
                  break;
              }
          }
      
          if (!employee_found)
          {
              printf("wrong number \n");
          }
      }
      (Notice that I fixed your for loop condition and added a break: you must loop for up to SIZE times, not employee_count number of times since the employee_count could be 1... with that valid employee being the last in the array.)


    Quote Originally Posted by hzkids
    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.
    This is probably due to your poor naming. You seemed to have used n1 here:
    Code:
    printf("Enter Employee ID: ");
    scanf("%d", &emp[n1].id);
    ... when you wanted to use i instead. If you had named n1 as employee_count instead, this mistake would have been obvious.
    Last edited by laserlight; 10-17-2018 at 03:15 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Edit and Delete struct records that are saved in file?
    By Supremezzy in forum C Programming
    Replies: 2
    Last Post: 05-04-2016, 10:31 AM
  2. Delete data from struct
    By John5 in forum C Programming
    Replies: 13
    Last Post: 03-14-2016, 11:40 AM
  3. Replies: 32
    Last Post: 03-30-2011, 12:26 PM
  4. delete memory in a struct destructor
    By JeremyCAFE in forum C++ Programming
    Replies: 5
    Last Post: 03-12-2006, 11:34 AM

Tags for this Thread