C++ 返回的进程-1073741819(0xC0000005)错误

C++ 返回的进程-1073741819(0xC0000005)错误,c++,codeblocks,C++,Codeblocks,我是一个编码初学者。我正在做一个小项目,从我的学院写一个停车系统。我使用双链表插入、搜索和删除来添加、搜索和删除系统中的车辆。在这个程序中,一切正常,直到我编译程序删除车辆。每当我输入车号将其从停车场删除时,就会发生错误,carparking.exe突然停止。 这是我的节目: #include <iostream> using namespace std; struct node { int no; int charge,entry_time,exit_time;

我是一个编码初学者。我正在做一个小项目,从我的学院写一个停车系统。我使用双链表插入、搜索和删除来添加、搜索和删除系统中的车辆。在这个程序中,一切正常,直到我编译程序删除车辆。每当我输入车号将其从停车场删除时,就会发生错误,carparking.exe突然停止。 这是我的节目:

#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;
}
#包括
使用名称空间std;
结构节点
{
国际贸易编号;
内部充电、进入时间、退出时间;
字符名[20];
节点*prev,*next;
};
班车
{
私人:
节点*头;
char-ch[20];
公众:
汽车()
{
head=NULL;
}
无效输入()
{

在函数
del_car()中的cout

如果只停了一辆车,则只有
head
有效,并且
head->next
head->prev
NULL

head = temp->next; // head becomes NULL here
head->prev = NULL;  //you are dereferencing a NULL pointer and hence the seg. fault
在这种情况下,只需设置
head=NULL


此外,如果有更多的车辆停放,则条件为
while(temp!=NULL)
,但如果找到了汽车,那么您必须中断循环,以便
温度不会不必要地增加。在
标志=true;
之后添加
中断;

请避免标记您未使用的语言,并通过删除演示问题不需要的代码来缩小问题范围。
head = temp->next; // head becomes NULL here
head->prev = NULL;  //you are dereferencing a NULL pointer and hence the seg. fault