In the output part, the pointer loses its place

Asked 2 years ago, Updated 2 years ago, 45 views

struct custom{
    custom* pLink;
    custom* pToFl;
    string name;

};
typedef struct portNode{
    int numberOfCus = 0;
    string flightName;
    custom headerNode;
    portNode* pLink;
}flight;

struct airport{
    int numberOfPort = 0;
    flight headerNode;
};

airport* createAirport(){
    airport* pReturn = NULL;

    pReturn = (airport*)malloc(sizeof(airport));

    if(pReturn != NULL){
        memset(pReturn, 0, sizeof(airport));
    }
    else{
        cout << "Memory allocation failure" << endl;
        return NULL;
    }
    return pReturn;
}

void addFlight(airport* pList, string newFlightName){
    flight* pNewFlight = NULL;
    flight* pPreNode = NULL;

    if(pList != NULL){
        pPreNode = &(pList->headerNode);
        while(pPreNode->flightName != newFlightName){
            if(pPreNode -> flightName != newFlightName && pPreNode -> pLink == NULL){
                pNewFlight = new flight;
                pNewFlight -> flightName = newFlightName;
                pNewFlight -> pLink = NULL;

                pNewFlight -> pLink = pPreNode -> pLink;
                pPreNode -> pLink = pNewFlight;
            }
            pPreNode = pPreNode->pLink;
            pList->numberOfPort++;
        }
    }
}

void addCustomer(airport* pList, string newFlightName, string newName){

custom* pNewCustom = NULL;
    custom* pPreNode = NULL;
    flight* pNewFlight = NULL;

        pNewCustom = new custom;
        if(pNewCustom != NULL){
            pNewCustom -> pLink = pNewCustom;
            pNewCustom -> pToFl = pNewCustom;
            pNewCustom -> name = newName;

            pNewFlight = &(pList->headerNode);

            while(pNewFlight->flightName != newFlightName){
                pNewFlight = pNewFlight -> pLink;
            }

            pPreNode = &(pNewFlight->headerNode);


            pNewCustom -> pToFl = pPreNode;
            pNewCustom -> pLink = pPreNode -> pLink;
            pPreNode -> pLink = pNewCustom;
        }
    }
}

----------------------------------------
void displayCustomer(airport* pList, string newFlightName){
flight* pNode = NULL;
custom* pCustom = NULL;

if(pList != NULL){
    pNode = (pList->headerNode.pLink);

    while(pNode->flightName != newFlightName){
        pNode = pNode -> pLink;
    }
    pCustom = pNode->headerNode.pLink;
    }
while(pCustom -> pLink != pCustom){
-----This is where the problem occurs--------
cout <<pCustom->name <<endl; //where Thread 1: EXC_BAD_ACCESS (code=1, address=0xc) ##Error code appears
    pCustom = pCustom->pLink;
}

}

int main() {
    airport* pList = NULL;
    int choice;
    string portName;
    string name;

    pList = createAirport();

    while(true){
        cout << "CHOICES:\n";
        cout << "1. Add an Airport\n";
        cout << "2. Find an Airport - Standard Looping\n";
        cout << "3. Exit Program\n";
        cout << "Enter your choice: ";
        cin >> choice; cout << "\n\n";

        switch(choice){
            case 1: cout << "Inserting Airport\n";
                cout << "portName: " << endl;
                cin >> portName;
                addFlight(pList, portName);
                cout << "name: " << endl;
                cin >> name;
                addCustomer(pList, portName, name);
                break;
            case 2: cout << "Finding Airport\n";
                cout << "portName: " << endl;
                cin >> portName;
                displayCustomer(pList, portName);
                break;
            case 3: cout << "Terminating......\n";
                exit(EXIT_SUCCESS);       break;
            default: cout << "Not Valid\n\n";break;
        }
    }
}

c++ pointer

2022-09-22 15:34

1 Answers

It will be easier to answer if you write a detailed explanation of what program you are planning and what difficulties you are experiencing.

Looking at the structure, it seems that various ports (flights) are stored as linked lists in one airport, and several custom ports (flights) are stored as linked lists.

When you run the program, you can enter the port (flight) name and custom name by pressing 1 at the main input, and when you press 2, you seem to be working on a program that prints all the custom names registered in the port (flight).

void display Customer (airport* pList, string newFlightName) {
    flight* pNode = NULL;
    custom* pCustom = NULL;

    if(pList != NULL){
        pNode = (pList->headerNode.pLink);

        while(pNode != NULL && pNode->flightName != newFlightName){
            pNode = pNode -> pLink;
        }
        If (pNode == NULL) // Exit the function if its flightName is not in the list
            return;
        else
            pCustom = pNode->headerNode.pLink;
    }
    while(pCustom!=NULL){ //Access to the last node in the list
        cout << pCustom->name << endl;
        pCustom = pCustom->pLink;
    }
}

I want you to fix it like this.


2022-09-22 15:34

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.