Thread: Access violation error?

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    1

    Access violation error?

    Hello all,

    I'm having an issue with this program -- it compiles and links just fine, but when I try to run it, it triggers an error that reads "Access violation error (segmentation fault) raised in your program."

    I'm at a complete loss -- any help would be greatly appreciated!

    DRIVER PROGRAM
    Code:
    #include "homework5.h"
    
    
    #include <iostream>
    
    
    using namespace std;
    
    
    int main()
    
    
    {
    List aList;
    int intArr[50]={0};
    char charArr[50]={0};
    //NodeChar *c1, *c2, *c3, *c4, *c5, *c6, *c7, *c8, *c9, *c10;
    //NodeInt *n1, *n2, *n3, *n4, *n5, *n6, *n7, *n8, *n9, *n10;
    
    
    NodeChar *charPtrArray[50]={0};
    NodeInt *intPtrArray[50]={0};
    
    
    char intchar=0;
    
    
    for (int x=0; x<10; x++)
    {
        cout<<"Enter 'i' for integer, or 'c' for character."<<endl;
        cin>>intchar;
        if (intchar=='c')
        {
        cout<<"Enter a character."<<endl;
        cin>>charArr[x];
        charPtrArray[x]->setANode(charArr[x]);
        aList.addItem(charPtrArray[x]);
        }
        else if (intchar=='i')
        {
        cout<<"Enter an integer."<<endl;
        cin>>intArr[x];
        intPtrArray[x]->setANode(intArr[x]);
        aList.addItem(intPtrArray[x]);
        }
        else
        { cout<<"You have made an invalid selection. Please choose 'i' or 'c'."<<endl;
        }
    }
        aList.printNodes();
        
    
    
         system("PAUSE");
         return 0;
    
    
    }
    HEADER FILE

    Code:
    #include <iostream>
    using namespace std;
    
    
    // The prototypes.
    
    
    class Node
    {
          private:
                 int newItem;
          public:
                 
                 Node *nextNode;
                 Node();
                 void setNextNode(Node*);
                 virtual void setANode(int)=0;
                 void getANode();
                 Node* getNextNode(); 
    };
    
    
    class NodeInt : public Node
    {
          private:
                  int newInt;
          public:
                 NodeInt();
                 //void setAnInt(int);
                 //void getANode();
                 virtual void setANode(int);
                 void getANode();
    };
    
    
    class NodeChar : public Node
    {
          private:
                  char newChar;
          public:
                 
                 int newInt;
                 NodeChar();
                 //void getANode();
                 virtual void setANode(char);
                 void getANode();
    };
    
    
    class List
    {
          public:
                  Node* top;
                  Node* tail;
                 List();
                 void setTop(Node*);
                 virtual bool addItem(Node*);
                 void printNodes();         
    };
    
    
    List::List()
    {          
         top=0;
         tail=0;
    }
    
    
    
    
    void List::printNodes()
    {
         Node* temp;
         temp=tail;
         while (tail !=NULL)
         {
               temp->getANode();
               tail=temp->getNextNode();
         }
    }
    
    
    NodeChar::NodeChar()
    {
    
    
    }
    
    
    NodeInt::NodeInt()
    {
     newInt=0;
    }
    
    
    Node* Node::getNextNode()
    {
     return nextNode;
    }
    
    
    void Node::setNextNode(Node* passedNode)
    {
         nextNode=passedNode;
    }
    
    
    bool List::addItem(Node* newNode)
    {
         newNode->setNextNode(tail);
         tail=newNode;
         return true;
    }
    
    
    void NodeChar::setANode(char theChar)
    {
       newChar=theChar;
    }
    
    
    void NodeChar::getANode()
    {
         cout<<newChar;
    }
    
    
    void NodeInt::getANode()
    {
         cout<<newInt;
    }
    
    
    void Node::getANode()
    {
         cout<<newItem;
    }
    
    
    void NodeInt::setANode(int theInt)
    {
       newInt=theInt;
    }
    
    
    Node::Node()
    {
     nextNode=0;
    }

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Code:
    bool List::addItem(Node* newNode)
    {
         newNode->setNextNode(tail);
         tail=newNode;
         return true;
    }
    If you do it this way the new tail points to the old tail, when it is supposed to be the other way round. So in the addItem function...

    Code:
    tail->setNextNode(newNode); // sets the current tail to point to the new tail.
    tail = newNode;             // update the current list object to reflect the new tail.
    tail->setNextNode(NULL);      // nothing comes after the tail.
    Seems about right.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    main function code is improperly indented.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Access violation error
    By kulfon in forum C++ Programming
    Replies: 2
    Last Post: 12-14-2011, 05:31 PM
  2. Access Violation Error
    By Neurofiend in forum C Programming
    Replies: 3
    Last Post: 08-22-2005, 06:19 PM
  3. Access Violation error
    By thedoofus in forum C Programming
    Replies: 2
    Last Post: 04-30-2005, 11:33 AM
  4. Access Violation Error
    By Scott in forum C++ Programming
    Replies: 6
    Last Post: 07-24-2003, 05:00 AM
  5. Help! Access violation error in C++
    By runlevel007 in forum C++ Programming
    Replies: 2
    Last Post: 03-18-2003, 07:56 PM