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;
}