'employee record using linked list segmentation fault problem c++

this code uses linked list to make employees records and there is many functions the user can make all of the function except when i use the deleterecord function if there is a record and I type in the id it shows me a segmentation fault output here

this is the deleterecord function

int deleterecord(){
  int id;
  Node *temp = new Node();
  temp = head;
  cout<<"insert employee id: "; 
  cin>>id;
    while(temp!=NULL){
      if(temp->next->id==id){
        temp->next=temp->next->next;
        cout<<"record has been deleted";
        return 0;
      }
      else{
       temp=temp->next; 
      }
  }
  return -1;
}

and this is the whole code using classes and linked list

#include <iostream>
using namespace std;
class Node{
public:
string name,firstDay,address;
int id,phone,Whours,salary;
Node *next;
};
class linkedlist{
Node* head;
public:
linkedlist(){head=NULL;}
Node* newList(){
  Node *head = new Node();
  return head;
}
void insertrecord(int id){
  string name,firstDay,address;
  int phone,Whours,salary;
  Node* newNode = new Node();
  Node *temp = new Node();
  temp = head;
  while(temp!=NULL){
    if(temp->id==id){
      cout<<"record already exist";
      return;
      }
    else{
      temp=temp->next;
    }
    }
  temp = head;
  cout<<"insert employee name: ";
  cin>>name;
  cout<<"insert employee first day of work: "; 
  cin>>firstDay;
  cout<<"insert employee phone number: "; 
  cin>>phone;
  cout<<"insert employee address: "; 
  cin>>address;
  cout<<"insert employee salary: "; 
  cin>>salary;
  newNode->name=name;
  newNode->id=id;
  newNode->firstDay=firstDay;
  newNode->phone=phone;
  newNode->address=address;
  newNode->Whours=32;
  newNode->salary=salary;
  
  if(head==NULL||head->id>=id){
    newNode->next=head;
    head=newNode;
    return;
    }
  while(temp->next!=NULL&&temp->next->id < id)
  {
    temp=temp->next;
  }
    
  newNode->next=temp->next;
  temp->next= newNode;
}
int deleterecord(){
  int id;
  Node *temp = new Node();
  temp = head;
  cout<<"insert employee id: "; 
  cin>>id;
    while(temp!=NULL){
      if(temp->next->id==id){
        temp->next=temp->next->next;
        cout<<"record has been deleted";
        return 0;
      }
      else{
       temp=temp->next; 
      }
  }
  return -1;
}
void updaterecord(){
  int n,input,id;
  string Input;
  cout<<"insert employee id: "; 
  cin>>id;
  Node *temp = new Node();
  temp = head;
  while(temp!=NULL){
    if(temp->id==id){
      do{
        cout<<"what do you like to update:\n1.name\n2.First day\n3.phone number\n4.address\n5.working hours\n6.salary\nenter -1 to cancel\n";
        cin>>n;
        if(n==1){
            cout<<"enter the new name: ";
            cin>>Input;
            temp->name=Input;
          }
          else if(n==2){
            cout<<"enter the new First day: ";
            cin>>Input;
            temp->firstDay=Input;
          }
          else if(n==3){
            cout<<"enter the new phone number: ";
            cin>>input;
            temp->phone=input;
          }
          else if(n==4){
            cout<<"enter the new address: ";
            cin>>Input;
            temp->address=Input;
          }
          else if(n==5||n==6){
            updatesalary(id);
          }
        else if(n==-1){
          return;
        }
      }while(n!=-1);
      }
    else{
    temp=temp->next;  
      }
    }
  cout<<"record does not exist";
  }
void ShowRecord(){
  Node *temp = new Node();
  temp = head;
  if(temp==NULL){
    cout<<"there is no records";
    return;
    }
  while(temp!=NULL){
  cout<<"name: "<<temp->name<<"\n"
  <<"ID: "<<temp->id<<"\n"
  <<"First day of work: "<<temp->firstDay<<"\n"
  <<"Phone number: "<<temp->phone<<"\n"
  <<"Address: "<<temp->address<<"\n"
  <<"Working hours: "<<temp->Whours<<"\n"
  <<"Salary: "<<temp->salary<<"\n";
      temp=temp->next;
    }
  }
void searchrecord(int id){
  Node *temp = new Node();
  temp = head;
  if(temp==NULL){
    cout<<"there is no records";
    return;
    }
  while(temp!=NULL){
  if(temp->id==id){
  cout<<"name: "<<temp->name<<"\n"
  <<"ID: "<<temp->id<<"\n"
  <<"First day of work: "<<temp->firstDay<<"\n"
  <<"Phone number: "<<temp->phone<<"\n"
  <<"Address: "<<temp->address<<"\n"
  <<"Working hours: "<<temp->Whours<<"\n"
  <<"Salary: "<<temp->salary<<"\n";
      return;
  }
  else{
   temp=temp->next;
  } 
}
  }
void updatesalary(int id){
  int twopercent,extraS,ew;
  Node *temp = new Node();
  temp = head;
  while(temp!=NULL){
    if(temp->id==id){
      cout<<"enter the extra working hours:";
      cin>>ew;
  twopercent=temp->salary*0.02;
  extraS = twopercent*ew;
  temp->salary=temp->salary+extraS;
      cout<<"salary has been updated\n";
      return;
      }
    }
  cout<<"record does not exist";
  }
};

int main() {
linkedlist records;
  int n,id;
  while(n!=7){
    cout<<"\nplease choose the operation you like to make:\n1.Insert employee record\n2.Delete employee record\n3.update employee record\n4.show employee record\n5.search employee record\n6.update employee salary\n7.Exit\n";
    cin>>n;
    if(n==1){
      cout<<"insert employee id: "; 
      cin>>id;
      records.insertrecord(id);
    }
    else if(n==2){
      int result;
      result=records.deleterecord();
      if(result==0){
        cout<<"record has been deleted";
      }
      else{
        cout<<"record does not exist";
      }
    }
    else if(n==3){
      records.updaterecord();
    }
    else if(n==4){
      records.ShowRecord();
    }
    else if(n==5){
     cout<<"insert employee id: "; 
    cin>>id;
      records.searchrecord(id);
    }
    else if(n==6){
     cout<<"insert employee id: "; 
    cin>>id;
      records.updatesalary(id);
    }
      }
  return 0;
}


Solution 1:[1]

  1. You can make a node and store the node to be deleted in it and than use delete function to delete the node as follows,

    int deleterecord(){ int id; Node *temp = new Node(); temp = head; Node *a; cout<<"insert employee id: "; cin>>id; if(temp->id==id) { a=temp; temp = temp->next; delete(a); return 0; } else { while(temp!=NULL){ if(temp->next->id==id){ a = temp->next; temp->next=temp->next->next; delete(a); return 0; break; } else{ temp=temp->next; } } } return -1; }

  2. Secondly, you are not checking for the condition if we want to delete 1 node that is a head node. Therefore, the "if" statement needs to be added to your code. The code I have suggested above clearly solves the problem.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sora_101