Access violation for Linked List Nodes: C++

-2

Need help writing a program for an airline reservation system that inputs and outputs the first name, last name, flight number, and boarding priority (Platinum, Gold, Silver, or Lead). The professor wants it done for a max of 10 passengers. This is the code I've written up so far.

It lets me enter in the information but once I type "D" to display it out, the program exits and I'm given the error: Access violation reading location 0x00000004

I'm not sure why this is happening and any help is appreciated. #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();
};

This is the .cpp file:

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");
}
c++
asked on Stack Overflow Mar 9, 2019 by icurryx3

1 Answer

0

This means you are trying to read heap memory you are not allowed to. Why do you comment out the while loop? Also, why do you not check in displayList if the head pointer is nullptr?

answered on Stack Overflow Mar 9, 2019 by Mini

User contributions licensed under CC BY-SA 3.0