Thread: Help with loops

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

    Help with loops


    problem where after removing employee and adding a new employee adds the employee at the end of the stack rather than deleted position.


    Code:
    #define
    Code:
     _CRT_SECURE_NO_WARNINGS
    #define SIZE 4
    #include<stdio.h>
    
    struct Employee
    {
        int id;
        int age;
        double salary;
    
    };
    int main(void)
    {
        int option;
        int i =0, j =0;
        int limit =0;
        int idsearch =0;
        int idsave =0;
        int count =0;
        structEmployee 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)
            {
            case0:
    
                printf("Exiting Employee Data Program. Good Bye!!! ");
    
                break;
            case1:
    
                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;
            case2:
    
                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;
            case3:
                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;
    
                        elseif(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;
    
    
    
                case4:
                        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;
                                }
                                elseif(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 =0;
                                    }
                                    
                                }
     limit -=1;
                            }
                        }while(count);
    
                        break;
    
            default:
                printf("ERROR: Incorrect Option: Try Again\n\n");
    
    
            }
        }while(option !=0);
    }
    


    am trying to add the new employee information after deleting a particular employee, but adds the new employee info at the end of the stack, instead of adding it to deleted position. I'm stuck on how to do it, please help me out
    Last edited by sumanthyj; 02-19-2019 at 02:00 PM.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    but adds the new employee info at the end of the stack
    Isn't that how a stack should act?

    Look at your delete code, what happens to limit when you "delete" a record?

    Now look at your "add" code, where does that code "add" the record?

    Perhaps if you want to "insert" a record at some arbitrary location you need to have some "insert" code.

  3. #3
    Registered User
    Join Date
    Feb 2019
    Posts
    5
    removing employee has to delete the employee information completely, so the stack has to omit the info.
    adding a new employee after deleting the employee has to update new information on deleted array position.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    removing employee has to delete the employee information completely,
    And how exactly can you do this?

    adding a new employee after deleting the employee has to update new information on deleted array position.
    Why?

    Did you run your program with your debugger and observe what is happening in the "delete" and "add" parts of your code?

    Did you try "printing" the "stack" after the "delete" but before the "add"? What did you observe?

  5. #5
    Registered User
    Join Date
    Feb 2019
    Posts
    5
    Attached output.
    Attached Images Attached Images Help with loops-1-jpg Help with loops-2-jpg 

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ programing , Loops , infinite loops issue
    By Ibrahim Lawleit in forum C++ Programming
    Replies: 15
    Last Post: 07-14-2014, 03:41 PM
  2. converting for loops to while loops
    By nabhatt in forum C Programming
    Replies: 3
    Last Post: 02-16-2012, 09:45 PM
  3. Replies: 3
    Last Post: 06-01-2011, 04:19 PM
  4. loops, menu loops
    By gloworm in forum C Programming
    Replies: 17
    Last Post: 04-12-2010, 07:59 PM
  5. while loops
    By neoistheone101 in forum C Programming
    Replies: 9
    Last Post: 11-15-2004, 08:01 PM

Tags for this Thread