Процесс вернул ошибку -1073741819 (0xC0000005) - PullRequest
0 голосов
/ 21 января 2020

Я новичок в кодировании. Я работаю над мини-проектом по созданию системы парковки из моего колледжа. Я использовал двусвязный список для вставки, поиска и удаления, чтобы добавлять, искать и удалять автомобили в системе. В этой программе все работает нормально, пока я не скомпилирую программу для удаления автомобиля. Всякий раз, когда я ввожу номер автомобиля, чтобы удалить его из При парковке возникает ошибка и 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;
}

1 Ответ

0 голосов
/ 21 января 2020

В функции del_car()

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

Если есть только припаркованный автомобиль, то действительными являются только 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), но если автомобиль найден , тогда вам нужно разбить l oop, чтобы temp не увеличивался без необходимости. Добавьте break; после flag=true;.

...