需要帮助为航空公司预订系统编写程序,该程序输入和输出名字,姓氏,航班号和登机优先级(白金,金,银或领队)。教授希望最多容纳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 << "\n\t(P)latinium\n";
cout << "\t(G)old\n";
cout << "\t(S)ilver\n";
cout << "\t(L)ead\n";
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: Platinium\n";
}
else if (nodePtr->Gold)
{
cout << "Priority: Gold\n";
}
else if (nodePtr->Silver)
{
cout << "Priority: Silver\n";
}
else if (nodePtr->Lead)
{
cout << "Priority: Lead\n";
}
else
{
cout << "None of the above.\n";
}
nodePtr = nodePtr->next;
//}
/*switch (response[i])
{
case passengerInfo::Platinium:
cout << "Priority: Platinium\n";
break;
case passengerInfo::Gold:
cout << "Priority: Gold\n";
break;
case passengerInfo::Silver:
cout << "Priority: Silver\n";
break;
case passengerInfo::Lead:
cout << "Priority: Lead\n";
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 << "\n\t(P)latinium\n";
cout << "\t(G)old\n";
cout << "\t(S)ilver\n";
cout << "\t(L)ead\n";
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");
}
答案 0 :(得分:0)
这意味着您正在尝试读取不允许的堆内存。为什么要注释掉while循环?另外,为什么不检查displayList
头指针是否为nullptr?