[英]Process returned -1073741819 (0xC0000005) error
我是编码的初学者。 我正在做一个小项目来编写我大学的停车场系统。 我使用双向链表插入、搜索和删除以在系统中添加、搜索和删除汽车。在这个程序中,一切正常,直到我编译程序以删除汽车。每当我输入车号将其从中删除停车时出现错误,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;
}
在函数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)
,但如果找到汽车,则必须打破循环,这样temp
就不会不必要地增加。 加个break;
在flag=true;
.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.