Thread: How to enter new value to a deleted string

  1. #1
    Registered User
    Join Date
    Feb 2019
    Posts
    5

    Talking How to enter new value to a deleted string

    I am stuck with logic on how to place the new array value to deleted position


    The below code can remove an employee and add new employee information at the bottom of the stack, however it has to add the employee to the deleted position.

    Code:
    
    #define _CRT_SECURE_NO_WARNINGS
    #define SIZE 4
    #include <stdio.h>
    
    
    struct Employee
    {
        int id;
        int age;
        double salary;
    
    
    };
    int main(void)
    {
        int option,flag=0;
        int i = 0, j = 0;
        int limit = 0;
        int idsearch = 0;
        int idsave = 0;
        int count = 0;
        struct Employee emp[SIZE] = { 0,0,0.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:
    
    
                printf("Exiting Employee Data Program. Good Bye!!! ");
    
    
                break;
            case 1:
    
    
                printf("EMP ID  EMP AGE EMP SALARY\n");
                printf("======  ======= ==========\n");
                for (i = 0;i < SIZE;i++)
                {
                    if (emp[i].id > 0)
                        printf(" % 6d % 9d% 11.2lf", emp[i].id, emp[i].age, emp[i].salary);
                    printf("\n");
    
    
                }
                printf("\n");
                break;
            case 2:
    
    
                printf("Adding Employee\n");
                printf("===============\n");
                if (limit < SIZE)
                {
                    printf("Enter Employee ID: ");
                    scanf("%d", &emp[limit].id);
    
    
                    printf("Enter Employee Age : ");
                    scanf("%d", &emp[limit].age);
    
    
                    printf("Enter Employee Salary : ");
                    scanf("%lf", &emp[limit].salary);
                    limit++;
                }
                
                else
                    printf(" ERROR!!! Maximum Number of Employees Reached \n");
                printf("\n");
                break;
            case 3:
                printf("Update Employee Salary\n");
                printf("======================\n");
    
    
                do
                {
                    count = 1;
                    printf("Enter Employee ID: ");
                    scanf("%d", &idsearch);
            
                    for (i = 0;i < limit;i++)
                    {
                        if (emp[i].id == idsearch)break;
    
    
                        else if (i == limit - 1)
                        {
                            printf("*** ERROR: Employee ID not found!***\n");
                        }
                    }
                        if (i != limit)
                        {
                            printf("The current salary is %.2f\n", emp[i].salary);
                            printf("Enter Employee New Salary: ");
                            scanf("%lf", &emp[i].salary);
                            printf("\n");
                            count = 0;
                        }
                    
                } while (count);
    
    
                break;
    
    
    
    
    
    
                case 4:
                        printf("Remove Employee\n");
                        printf("===============\n");
                        do
                        {
                            count = 1;
                            printf("Enter Employee ID: ");
                            scanf("%d", &idsearch);
                            
                            for (i = 0;i < limit;i++)
                            {
    
    
                                if (emp[i].id==idsearch)
                                {
                                    break;
                                }
                                else if (i == limit - 1)
                                {
                                    printf("*** ERROR: Employee ID not found!***\n");
                                }
                            }
                            if (i != limit)
                            {
                                count = 0;
                                idsave = i;
                                printf("Employee %d will be removed\n\n", emp[idsave].id);
                                
                                for (i = idsave;i < limit;i++)
                                {
                                    if (i != limit - 1)
                                    {
                                        emp[i] = emp[i+1];
                                        
                                    }
                                    else
                                    {
                                        emp[i].id = -1;
                                    }
                                    
                                }
                                limit -= 1;
                            }
                            
                            
                        } while (count);
                        
                        break;
    
    
            default:
                printf("ERROR: Incorrect Option: Try Again\n\n");
    
    
    
    
            }
        } while (option != 0);
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    1. Your remove function only needs to do this for the removed employee. You don't need to move any other elements in your array.
    emp[i].id = -1;

    2. Your add function needs to search for the FIRST emp[i].id == -1 slot and add the employee there.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. HELP! How To Enter Spaces into a string
    By Rodrigo Marques in forum C Programming
    Replies: 3
    Last Post: 12-13-2016, 07:37 AM
  2. Replies: 3
    Last Post: 01-28-2015, 11:35 PM
  3. how,without a ENTER to finish the string-input?
    By procs in forum C Programming
    Replies: 2
    Last Post: 05-05-2010, 08:26 AM
  4. user input pre-enter string?
    By mikeyp in forum C++ Programming
    Replies: 4
    Last Post: 11-25-2009, 02:59 PM
  5. Replies: 9
    Last Post: 02-22-2009, 11:50 PM

Tags for this Thread