Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

c++ problem with function

Options
  • 17-04-2003 10:07pm
    #1
    Registered Users Posts: 2,593 ✭✭✭


    hi all
    i having a prob with this function i ment to take a linked list of gates that have been added and connected but when i try to get the value for second gate there is a read of an invalid address the structures are shown below as well
    it read the first gate properly and carries out the simulation but when then second gate is read there is an error at
    inputvalue = gatelist->Gate->connectorlist->ptr_to_connector->Value;

    this for my 3rd year proj and any help would be greatly appreciated

    int Simulation(GateList *gatelist)
    {//open addgate
    InputList* ValuesList = NULL;

    int value,typenum = 0,inputvalue = 0;
    char typegate[10];

    while (gatelist != NULL)
    {// open while
    strcpy(typegate,gatelist->Gate->Type);

    while(gatelist->Gate->connectorlist != NULL)
    { // check that the values are connected and that they value is accessible
    inputvalue = gatelist->Gate->connectorlist->ptr_to_connector->Value;
    InputList *newnode = NULL;
    newnode = getInputList();
    newnode->NextInput = NULL;
    newnode->input = inputvalue;
    newnode->NextInput = ValuesList;
    ValuesList = newnode;
    gatelist->Gate->connectorlist = gatelist->Gate->connectorlist->nextconnectorlist;
    }
    if (strcmp(typegate,"And")== 0)
    typenum = 1;
    else if (strcmp(typegate,"Or")== 0)
    typenum = 2;
    else if (strcmp(typegate,"Not")== 0)
    typenum = 3;
    else if (strcmp(typegate,"Nand")== 0)
    typenum = 4;
    else if (strcmp(typegate,"Nor")== 0)
    typenum = 5;
    else if (strcmp(typegate,"Xor")== 0)
    typenum = 6;
    else if (strcmp(typegate,"Xnor")== 0)
    typenum = 7;
    else
    typenum = 8;
    // choose which gate function that you need using the gate type
    switch (typenum)
    { //open switch
    case 1:
    value = AndFunction(ValuesList);
    //change and function
    break;
    case 2:
    value = OrFunction(ValuesList);
    break;
    case 3:
    value = NotFunction(ValuesList);
    break;
    case 4:
    value = NandFunction(ValuesList);
    break;
    case 5:
    value = NorFunction(ValuesList);
    break;
    case 6:
    value = XorFunction(ValuesList);
    break;
    case 7:
    value = XnorFunction(ValuesList);
    break;
    case 8:
    Application->MessageBox(" unknown error !!! ", "Welcome to Logic Gate Simulator", MB_OKCANCEL + MB_DEFBUTTON1);
    break;
    }//close switch
    if( gatelist->Gate->NextConnector != NULL)
    gatelist->Gate->NextConnector->Value = value;

    gatelist = gatelist->NextGate;
    }// close while
    return(value);
    }


    struct Connector_node{

    int Value; // value on the line
    struct Connectorlist *ptr_to_gate; //pointer to a node of connectorlist
    struct Connector_node *nextPtr; // pointer to the next connector type in the linked list
    };

    struct Connectorlist{

    struct Connectorlist *nextconnectorlist; // pointer to the next connector type in the linked list
    struct Connector_node *ptr_to_connector; // pointer to the next connector type in the linked list
    };

    struct Gate_node{

    int XPos; // location of the gate (i.e node) on the screen
    int YPos; // location of the gate (i.e node) on the screen
    int NumInputs; // number of inputs to the gate
    char Type[10]; // type of gate
    struct Connector_node *NextConnector; // the pointer to the next connector node
    Connectorlist *connectorlist; // the pointer to the connectorlist
    };

    struct GateList{

    struct Gate_node *Gate; // pointer to the gate
    struct GateList *NextGate; // pointer to next
    };

    struct InputList{
    int input; // input value
    struct InputList *NextInput; // pointer to next input
    };

    tanx


Comments

  • Registered Users Posts: 6,240 ✭✭✭hussey


    You are checking this value :
    while(gatelist->Gate->connectorlist != NULL)

    you are not checking this
    gatelist->Gate->connectorlist->ptr_to_connector->Value;

    ptr_to_connector could be null??
    value might not be set correctly??

    Not 100% sure, what error are you getting


  • Registered Users Posts: 2,593 ✭✭✭tommycahir


    hi hussey i got an answer from another forum tanx for the help the error was that it kept trying to read an address that wasnt valid. got the whole thing running cos of that now only have to read and write a full data structure to file !!! any good sites that might help with this would be appreciated..
    tanx for the help


  • Closed Accounts Posts: 7 issch


    How come you use a switch statement to choose which function to call rather than calling them from your if statements?


  • Registered Users Posts: 2,593 ✭✭✭tommycahir


    actually that what i have done since i posted this what i had there was only a rough model i have tided up the code since then.


Advertisement