Process returned -1073741819 (0xC0000005) error

-4

I'm a beginner in coding. I am woring on a mini project to write a car parking system from my college. I used doubly-linked list to insert, search and delete to add, search and remove cars in the system .In this program, everything works fine untill i compile the program to remove a Car.Whenever i enter the car number to remove it from parking the error occurs and the carparking.exe suddenly stops. This is my program:

#include <iostream>

using namespace std;

struct node
{
    int no;
    int charge,entry_time,exit_time;
    char name[20];
    node *prev, *next;
};
class Car
{
private:
    node *head;
    char ch[20];
public:
    Car()
    {
        head= NULL;
    }
    void input()
    {
        cout<<"\nWelcome to Car Parking system\n"<<endl;
        cout<<"\n Press 1: To Park a Car"<<endl;
        cout<<"\n Press 2: To Search a Car"<<endl;
        cout<<"\n Press 3: To Remove of Car"<<endl;
        cout<<"\n Press 4: To display the Parking records"<<endl;
        cout<<"\n Press 5: Exit the program"<<endl;
        return;
    }
    void add_car()
    {
        node *newer= new node;
        system("cls");
        cout<<"Enter the Car number: "<<endl;
        cin>>newer->no;
        fflush(stdin);
        cout<<"Enter the name of driver: "<<endl;
        cin.getline(newer->name,20);
        fflush(stdin);
        cout<<"Enter the entry time of car"<<endl;
        cin>>newer->entry_time;
        fflush(stdin);
        cout<<"Enter the exit time of car"<<endl;
        cin>>newer->exit_time;
        fflush(stdin);
        newer->charge=(newer->exit_time-newer->entry_time)*300;
        newer->next =head;
        newer->prev =NULL;
        if(head!=NULL)
        {
            head->prev = newer;
        }
        head= newer;
        cout<<"\nThe car is parked successfully"<<endl;
        if(head==NULL)
        {
            cout<<"\n No Car is parked: "<<endl;
        }
    }
    void del_car()
    {
        if(head==NULL)
        {
            cout<<"\n No Car is parked "<<endl;
        }
        else
        {
            int value;
            cout<<"\nEnter the Car number to move"<<endl;
            cin>>value;
            node *temp=head;
            bool flag=false;
            if(temp->no== value)
            {
                head=temp->next;
                head->prev=NULL;
                flag= true;
                delete temp;
                if(flag==true)
                {
                    cout<<"\nThe Parking charge is Rs "<<temp->charge<<" only"<<endl;
                    cout<<"The car is moved out of parking zone"<<endl;
                }
            }
            else
            {
                while (temp!=NULL)
                {
                    if(temp->no==value)
                    {
                        node *p,*q;
                        if(temp->next==NULL)
                        {
                            p=temp->prev;
                            p->next=NULL;
                            flag=true;
                            delete temp;
                        }
                        else
                        {
                            p=temp->prev;
                            q=temp->next;
                            p->next=q;
                            q->prev=p;
                            flag=true;
                            delete temp;
                        }
                    }
                    temp=temp->next;
                }
                if(flag==false)
                {
                    cout<<"\n\t The car number is not found"<<endl;
                }
            }
        }
    }
    void display()
    {
         if(head==NULL)
        {
            cout<<"No Car is parked"<<endl;
        }
        else
        {
            node *temp= head;
            while(temp!= NULL)
            {
                cout<<"\n------------------------------Information----------------------------------------"<<endl;
                cout<<"\n\tThe name of the driver is:"<<temp->name<<endl;
                cout<<"\n\tThe Car number is: "<<temp->no<<endl;
                cout<<"\n\tThe entry time of the car is: "<<temp->entry_time<<endl;
                cout<<"\n\tThe exit time of the car is: "<<temp->exit_time<<endl;
                temp =temp->next;
            }
        }
    }
    void search()
    {
        if(head==NULL)
        {
            cout<<"No Car is parked"<<endl;
        }
        else
        {
          int value;
          cout<<"\nEnter the car number to search"<<endl;
          cin>>value;
          node *temp=head;
          bool flag=false;
          if(temp->no=value)
          {
              cout<<"\n\t----Information of the Car-----"<<endl;
              cout<<"\nThe name of the driver is: "<<temp->name<<endl;
              cout<<"\nThe Car number is: "<<temp->no<<endl;
              cout<<"\nThe entry time is: "<<temp->entry_time<<endl;
              cout<<"\nThe Car is still Parked "<<endl;
              cout<<"\nThe Parking charge is still pending "<<endl;
              return;
          }
          temp= temp->next;
        }
    }
};
int main()
{
    int n;
    string ch;
    Car c1;
    x2:
        c1.input();
        cout<<"\n\t----Enter your choice----"<<endl;
        cin>>n;
        if(n==1)
        {
            x1:
            c1.add_car();
            cout<<"\nDo you want to go to Main Menu: Press(y or n)"<<endl;
            cin>>ch;
            fflush(stdin);
            if(ch=="Y" || ch=="y")
            {
                goto x2;
            }
            else
            {
                exit(1);
            }
        }
        else if(n==2)
        {
         c1.search();
            cout<<"\nDo you want to go to Main Menu: Press(y or n)"<<endl;
            cin>>ch;
            if(ch=="Y"||ch=="y")
            {
                goto x2;
            }
            else
            {
                exit(1);
            }
        }
        else if(n==3)
        {
            c1.del_car();
            cout<<"\nDo you want to go to Main Menu: Press(y or n)"<<endl;
            cin>>ch;
            if(ch=="Y"||ch=="y")
            {
                goto x2;
            }
            else
            {
                exit(1);
            }
        }
        else if(n==4)
        {
            c1.display();
            cout<<"\nDo you want to go to Main Menu: Press(y or n)"<<endl;
            cin>>ch;
            if(ch=="Y"||ch=="y")
            {
                goto x2;
            }
            else
            {
                exit(1);
            }
        }
        else if(n==5)
        {
            exit(1);
        }
        else
        {
            cout<<"\n\tChoose correct number"<<endl;
            goto x2;
        }
        return 0;
}
c++
codeblocks
asked on Stack Overflow Jan 21, 2020 by Sulav Thapa • edited Jan 21, 2020 by ikegami

1 Answer

0

In the function del_car()

    if(temp->no== value)
    {
        head=temp->next;
        head->prev=NULL;
    }

If there is only car parked, then only head is valid and head->next and head->prev will be NULL. Now,

head = temp->next; // head becomes NULL here
head->prev = NULL;  //you are dereferencing a NULL pointer and hence the seg. fault

Just setting head = NULL should do in this condition.

Also, if there are more cars parked, the condition is while(temp!=NULL), but if the car is found, then you have to break the loop so that temp won't be incremented unnecessarily. Add a break; after flag=true;.

answered on Stack Overflow Jan 21, 2020 by Wander3r

User contributions licensed under CC BY-SA 3.0