链接列表节点的访问违规:C

Access violation for Linked List Nodes: C++

本文关键字:访问 列表 节点 链接      更新时间:2023-10-16

需要帮助为航空公司预订系统编写程序,该程序输入和输出名字,姓氏,飞行号和登机优先级(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