Click here to Skip to main content
15,915,019 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
After inserting employee name the code is stopped working.
Please solve this problem i will keep the whole code below.

What I have tried:

C++
#include <stdio.h>  
     #include <stdlib.h>
     #include<malloc.h>

typedef struct emp

{  
    int id;
    char nm;
    struct emp *prev;
    struct emp *next;
}list;
  list *start;
  list *end;

///////////////////////////////////// Functios /////////////////////////////////////

void initlist (void);
struct emp *create_emp(void);    
void append_emp( struct emp *element);    
int  delete_emp(int d);    
void edit_emp(int old , int current);    
list *display ();    
struct emp *search_emp(int s);  

//////////////////////////////////////////////////////////////////////
///////////////////////////////////// MAIN ////////////////////////////
//////////////////////////////////////////////////////////////////////

int main()
{   
int cases;
char nm1;int id1;list *c1; list *i1;    
int c2,del;
int old3,new3;
int i5;list *c5;


initlist();

    printf("\nLinked List for Employees\n");
    printf("---------------------------------------------\n");

    printf("Press 1 to INSERT an Employee into the list \n");
    printf("Press 2 to DELETE an Employee from the list \n");
    printf("Press 3 to EDIT Employee \n");
    printf("Press 4 to DISPLAY the list \n");    
    printf("Press 5 to SEARCH the list \n");    
    printf("Press 6 to EXIT the program\n");    
    printf("---------------------------------------------\n");

    while (1)
        {
            scanf("%d",&cases);
           if (cases ==6)
            {
                break;
            }

           switch(cases) 
           { 
               case 1: //why when i run this code it's stop working   
                           c1=create_emp();
                           append_emp(c1);    
                           printf("Enter the new name : ");    
                           scanf("%c\n",& nm1);    
                           printf("Enter the new id : ");    
                           scanf("%d\n",& id1);    
                           i1 ->id =id1;
                           c1->nm=nm1;
                break;


                case 2: //why when i run this code it's stop working

        printf("Enter the Employee id you want to delete\n");

                            scanf("%d\n",c2);    
                            del=delete_emp(c2);    
                            if (del == 0)    
                            {    
                                printf("Not found");
                            }    
                            else    
                            {    
                                printf("Removed form list");
                            }   
                break;


                case 3: //why when i run this code it's stop working

                            printf("Enter the Employee id you want to Edit \n");
                            scanf("%d",old3);    
                            printf("Enter the new Employee id");    
                            scanf("%d",new3);    
                            edit_emp(old3,new3);    
                break;       

                case 4:
                            display();    
                break;   

                case 5:    
                            printf("Enter the id you'r searching for :");    
                            scanf("%d",i5);    
                            c5=search_emp(i5);    
                            if(c5==NULL)    
                            printf("not found\n");    
                            else    
                            printf("found\n");        
                break;  

           }
        }
   }

///////////////////////////////////// Creat list ///////////////////////////

void initlist (void)
{
    start=end=NULL;
}



    ///////////////////////////////////// Create emp /////////////////////////////////////
struct emp *create_emp(void)
{
    return ((struct emp*)malloc(sizeof(struct emp)));
}

///////////////////////////////////// Insert emp /////////////////////////////////////
void append_emp( struct emp *element )
{
    if ((start==NULL)&&(end==NULL))
    {
        start=element;
        end=element;
        element->next=NULL;
        element->prev=NULL;
    }
    else
    {
        end->next=element;
        element->prev=end;
        end=element;
        element->next=NULL;
    }
}

///////////////////////////////////// Delete emp /////////////////////////////////////
int  delete_emp(int d)
{
    struct emp *temp;
    temp=search_emp (d);
    if(temp==NULL)
    return 0;
    if(start==end)
    {
        start=NULL;
        end=NULL;
        free(temp);
        return 1;
    }
    else if(temp==start)
    {
        start=start->next;
        start->prev=NULL;
        free(temp);
        return 1;
    }
    else if (temp==end)
    {
        end=temp->prev;
        end->next=NULL;
        free(temp);
        return 1;
    }
    else
    {
        temp->prev->next=temp->next;
        temp->next->prev=temp->prev;
        free(temp);
        return 1;
    }
}

///////////////////////////////////// Edit emp /////////////////////////////////////
void edit_emp(int old , int current)
{
    list * temp;
    temp = search_emp(old);
    if (temp==NULL)
    {
        printf("NOT EXIST\n");
    }
    else
    {
        temp->id =current;
    }
}

///////////////////////////////////// Display list /////////////////////////////////////
list *display ()
{
    int d=1;
    list*temp;
    temp=start;
    while (temp!=NULL)
    {
        printf("The Employee %d ID is : %d\n",d,temp->id);
        temp=temp->next;
        d++;
    }
}

///////////////////////////////////// Search dy ID /////////////////////////////////////
struct emp *search_emp(int s)
{
    struct emp *temp;
    temp=start;
    while((temp!=NULL)&&(temp->id!=s))
    {
        temp=temp->next;
    }
    return temp;
}
Posted
Updated 19-Sep-18 1:27am
v2
Comments
phil.o 19-Sep-18 7:13am    
Debug your code, as you have been told by OG in his solution. No one here will want to do this for you; moreover, you may find that it is a rather interesting activity.

Your list * i1 is not created:

C++
switch(cases)
{
    case 1:    //why when i run this code it's stop working
                c1=create_emp();
                append_emp(c1);
                printf("Enter the new name : ");
                scanf("%c\n",& nm1);
                printf("Enter the new id : ");
                scanf("%d\n",& id1);

                // because you are missing this part:
                i1 = create_emp();

                i1->id =id1;
                c1->nm=nm1;
     break;


And do you know how have I found it? I have debugged your code!
Maybe you should do it next time by your self? :)
 
Share this answer
 
v3
Comments
Member 13990067 19-Sep-18 10:43am    
thanks for your concern!!
once execute it !! i had got to enter name trice so i am not getting exact output
Member 13990067 20-Sep-18 4:13am    
had anyone done it?
Compiling does not mean your code is right! :laugh:
Think of the development process as writing an email: compiling successfully means that you wrote the email in the right language - English, rather than German for example - not that the email contained the message you wanted to send.

So now you enter the second stage of development (in reality it's the fourth or fifth, but you'll come to the earlier stages later): Testing and Debugging.

Start by looking at what it does do, and how that differs from what you wanted. This is important, because it give you information as to why it's doing it. For example, if a program is intended to let the user enter a number and it doubles it and prints the answer, then if the input / output was like this:
Input   Expected output    Actual output
  1            2                 1
  2            4                 4
  3            6                 9
  4            8                16
Then it's fairly obvious that the problem is with the bit which doubles it - it's not adding itself to itself, or multiplying it by 2, it's multiplying it by itself and returning the square of the input.
So with that, you can look at the code and it's obvious that it's somewhere here:
C#
int Double(int value)
   {
   return value * value;
   }

Once you have an idea what might be going wrong, start using the debugger to find out why. I can't tell you how to use your debugger - I have no idea which ones you have available to you - but a quick Google with the name of your IDE and the word "debugger" will almost certainly get you a tutorial. Put a breakpoint on the first line of the method, and run your app. When it reaches the breakpoint, the debugger will stop, and hand control over to you. You can now run your code line-by-line (called "single stepping") and look at (or even change) variable contents as necessary (heck, you can even change the code and try again if you need to).
Think about what each line in the code should do before you execute it, and compare that to what it actually did when you use the "Step over" button to execute each line in turn. Did it do what you expect? If so, move on to the next line.
If not, why not? How does it differ?
Hopefully, that should help you locate which part of that code has a problem, and what the problem is.
This is a skill, and it's one which is well worth developing as it helps you in the real world as well as in development. And like all skills, it only improves by use!
 
Share this answer
 
For that common problem of issues in the code the debugger is made. Learn from this debugger tutorial how to use it.

I guess you will find a problem with your code like a copy or free on some invalid memory pointer.
 
Share this answer
 
Comments
KarstenK 20-Sep-18 7:44am    
Doing your work at the last moment is a bad strategy especially in coding. When finding problems so late you havent any time to solve the problem.

As I wrote read the tutorial and use the debugger. Downvoting people which are helping you is a strategy which will FAIL IN REAL LIFE !!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900