链接列表节点的访问违规:C
Access violation for Linked List Nodes: C++
需要帮助为航空公司预订系统编写程序,该程序输入和输出名字,姓氏,飞行号和登机优先级(Platinum,platinum,Gold,Silver或Lead)。这位教授希望它以最多10名乘客的身份完成。这是我到目前为止写的代码。
它使我可以输入信息,但是一旦我键入" D"以显示出来,程序就会退出,我给出了错误:访问违规读取位置0x00000004
我不确定为什么会发生这种情况,并感谢任何帮助。 #include
using namespace std;
class airPassenger
{
struct passengerInfo
{
int fltNum;
char fName[10];
char lName[10];
enum priority { Platinium, Gold, Silver, Lead };
passengerInfo *next = NULL;
};
passengerInfo* head;
public:
airPassenger() { head = NULL; };
passengerInfo maxPassengers[10];
passengerInfo::priority passengerP;
void addNode();
void topMenu();
void displayList();
};
这是.cpp文件:
int main() {
airPassenger obj;
obj.topMenu();
return 0;
}
void airPassenger::addNode()
{
passengerInfo* newNode, *nodePtr;
newNode = new passengerInfo;
char priority_Response;
int priorty_Response1;
cout << "nPassenger First Name: ";
cin.ignore();
cin >> newNode->fName;
cout << "nPassenger Last Name: ";
cin >> newNode->lName;
cout << "nFlight Number: ";
cin >> newNode->fltNum;
do {
cout << "nPriority: ";
cout << "nt(P)latiniumn";
cout << "t(G)oldn";
cout << "t(S)ilvern";
cout << "t(L)eadn";
cin >> priority_Response;
} while ((priority_Response != 'P') && (priority_Response != 'p') && (priority_Response != 'G') && (priority_Response != 'g') && (priority_Response != 'S') && (priority_Response != 's') && (priority_Response != 'L') && (priority_Response != 'l'));
if (priority_Response == 'P' || priority_Response == 'p')
{
newNode->Platinium;
//priorty_Response1 = 0;
}
else if (priority_Response == 'G' || priority_Response == 'g')
{
newNode->Gold;
//priorty_Response1 = 1;
}
else if (priority_Response == 'S' || priority_Response == 's')
{
newNode->Silver;
//priorty_Response1 = 2;
}
else if (priority_Response == 'L' || priority_Response == 'l')
{
newNode->Lead;
//priorty_Response1 = 3;
}
else
{
priorty_Response1 = -1;
}
if (!head)
{
head = newNode;
}
else
{
nodePtr = head;
while (nodePtr->next)
{
nodePtr = nodePtr->next;
}
nodePtr->next = newNode;
}
system("cls");
}
void airPassenger::displayList()
{
passengerInfo* nodePtr;
nodePtr = head;
//while (nodePtr != NULL)
//{
cout << "nFName: " << nodePtr->fName << endl;
cout << "LName: " << nodePtr->lName << endl;
cout << "Flt Num: " << nodePtr->fltNum << endl;
if (nodePtr->Platinium)
{
cout << "Priority: Platiniumn";
}
else if (nodePtr->Gold)
{
cout << "Priority: Goldn";
}
else if (nodePtr->Silver)
{
cout << "Priority: Silvern";
}
else if (nodePtr->Lead)
{
cout << "Priority: Leadn";
}
else
{
cout << "None of the above.n";
}
nodePtr = nodePtr->next;
//}
/*switch (response[i])
{
case passengerInfo::Platinium:
cout << "Priority: Platiniumn";
break;
case passengerInfo::Gold:
cout << "Priority: Goldn";
break;
case passengerInfo::Silver:
cout << "Priority: Silvern";
break;
case passengerInfo::Lead:
cout << "Priority: Leadn";
break;
default:
cout << "None of the above.n";
break;
}*/
cout << endl;
}
void airPassenger::topMenu()
{
airPassenger passenger;
int priorty_Response1[10];
int counter = 0;
char usr_Response = 'A';
char priority_Response[10];
while (usr_Response != 'Q' && usr_Response != 'q')
{
cout << "(E)nter the passenger information" << endl;
cout << "(D)isplay the passenger information" << endl;
cout << "(Q)uit the program" << endl;
cout << "Which option would you like?: ";
cin >> usr_Response;
if (usr_Response == 'E' || usr_Response == 'e')
{
passenger.addNode();
//cout << "nPassenger First Name: ";
//cin >> maxPassengers[counter].fName;
//cout << "nPassenger Last Name: ";
//cin >> maxPassengers[counter].lName;
//cout << "nFlight Number: ";
//cin >> counter;
//cin >> maxPassengers[counter].fltNum[counter];
/*cout << "nPriority: ";
cout << "nt(P)latiniumn";
cout << "t(G)oldn";
cout << "t(S)ilvern";
cout << "t(L)eadn";
cin >> priority_Response[counter];
if (priority_Response[counter] == 'P' || priority_Response[counter] == 'p')
{
priorty_Response1[counter] = 0;
}
else if (priority_Response[counter] == 'G' || priority_Response[counter] == 'g')
{
priorty_Response1[counter] = 1;
}
else if (priority_Response[counter] == 'S' || priority_Response[counter] == 's')
{
priorty_Response1[counter] = 2;
}
else if (priority_Response[counter] == 'L' || priority_Response[counter] == 'l')
{
priorty_Response1[counter] = 3;
}
else
{
priorty_Response1[counter] = -1;
}
counter++;
system("CLS");*/
}
else if (usr_Response == 'D' || usr_Response == 'd')
{
displayList();
}
cin.ignore();
}
cout << endl;
system("PAUSE");
}
这意味着您正在尝试读取不允许的堆内存。您为什么要发表评论时循环?另外,为什么如果头指针为nullptr,为什么不检查displayList
?
相关文章:
- 访问列表中的所有 std::变体,并为每次访问捕获附加值
- 访问列表向量中的结构
- 在 c++ 中访问列表向量时出现问题
- C++娜娜 如何从全局列表框*指针访问列表框内的索引 0 上的按钮?
- 从C++访问列表视图委托的文本
- 尝试访问列表时出错
- Qt:通过深度复制访问列表中的数据结构是否应该比通过指针访问它慢得多
- Boost::p tree - 访问列表中包含的属性树节点
- 循环访问列表视图项 win32 api
- 如何访问列表中的数据成员,具有与我的列表不同的类型
- 从C++访问列表元素 (QML) 颜色
- 用于随机访问列表的STL容器
- 如何在c++中访问列表中的列表
- 通过 getter 访问列表的元素与直接访问类成员给出了不同的答案
- 结构中的 c++ 访问列表
- 访问列表项时的 UI 和工作线程同步
- 循环访问列表映射
- 如何在 RCPP 中访问列表
- 父类中的类访问列表
- 这是访问列表中对象的正确方式吗