I'd like to thank everyone for their help yesterday and the day before (Daved, you'll see that I did go to a simpler interface, without any atoi conversions to worry about). The problem I have now is this (thought I'd create a new thread for this problem): 'object reference set to an instance of an object' upon execution. The program compiles, but crashes when I try to add a line (using insertLine() function). It does not like me creating a temp linkedListNode object and trying to add the new text to its 'value' field via cin.getline(). The thought was this: set the temp node equal to head, then increment (set it equal to the next node via temp = temp->next) until reaching the node before the insertion point, then tie in the secondTmp temp node. Relevant code portions below, and then whole code posted. Thanks in advance for any help/advice!
class declarations:
Code:
class linkedListNode
{
public:
char *value;
linkedListNode *nextPtr;
linkedListNode(char *val = "", linkedListNode *nxtPtr = 0)
{
nextPtr = nxtPtr;
value = val;
}
linkedListNode(linkedListNode *nextPtr)
{
nextPtr = NULL;
}
};
class linkedList
{
public:
linkedList()
{
headPtr = tailPtr = 0;
}
~linkedList();
bool isEmpty()
{
return headPtr == 0;
}
void insertLine(int);
void addToHead();
void addToTail();
void displayList();
void deleteFromHead();
void deleteNode();
void deleteLines();
void addTextToLine();
void deleteFromTail();
private:
linkedListNode *headPtr, *tailPtr;
};
insertLine() function (the int it is passed = line number before which input is to be placed, based on user input)
Code:
void linkedList::insertLine(int num)
{
linkedListNode *tmp, *secondTmp;
cout << "OK, enter the text to be added to line " << num << endl;
cin.getline(secondTmp->value, 255); //here's where it all goes bad
int i;
if(num == 1)
{
headPtr = tailPtr = tmp;
}
else
{
tmp = headPtr;
for(i = 1; i < num; i++)
{
tmp = tmp->nextPtr;
}
}
tmp->nextPtr = secondTmp;
secondTmp->nextPtr = tmp->nextPtr->nextPtr;
}
__________________________________________________ ______________
whole program:
Code:
#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
using namespace std;
class linkedListNode
{
public:
char *value;
linkedListNode *nextPtr;
linkedListNode(char *val = "", linkedListNode *nxtPtr = 0)
{
nextPtr = nxtPtr;
value = val;
}
linkedListNode(linkedListNode *nextPtr)
{
nextPtr = NULL;
}
};
class linkedList
{
public:
linkedList()
{
headPtr = tailPtr = 0;
}
~linkedList();
bool isEmpty()
{
return headPtr == 0;
}
void insertLine(int);
void addToHead();
void addToTail();
void displayList();
void deleteFromHead();
void deleteNode();
void deleteLines();
void addTextToLine();
void deleteFromTail();
private:
linkedListNode *headPtr, *tailPtr;
};
linkedList::~linkedList()
{
for(linkedListNode *p; !isEmpty(); )
{
p = headPtr->nextPtr;
delete headPtr;
headPtr = p;
}
}
void linkedList::addToHead()
{
char headTxt[255];
cout << "Please enter the text to be inserted into the first line: " << endl;
cin.getline(headTxt, 255);
headPtr = new linkedListNode(headTxt, headPtr);
if(tailPtr == 0)
tailPtr = headPtr;
}
void linkedList::addToTail()
{
linkedListNode *temp, *nextTemp;
char input[255];
cout << "Please enter the line of text to be added: ";
cin.getline(input,255);
cin.ignore();
temp = new linkedListNode();
temp->nextPtr = NULL;
if (headPtr == NULL)
{
headPtr = temp;
}
else
{
nextTemp = headPtr;
while (nextTemp->nextPtr != NULL)
{
nextTemp = nextTemp->nextPtr;
}
nextTemp->nextPtr = temp;
}
}
void linkedList::insertLine(int num)
{
linkedListNode *tmp, *secondTmp;
cout << "OK, enter the text to be added to line " << num << endl;
cin.getline(secondTmp->value, 255);
int i;
if(num == 1)
{
headPtr = tailPtr = tmp;
}
else
{
tmp = headPtr;
for(i = 1; i < num; i++)
{
tmp = tmp->nextPtr;
}
}
tmp->nextPtr = secondTmp;
secondTmp->nextPtr = tmp->nextPtr->nextPtr;
}
void linkedList::deleteFromHead()
{
linkedListNode *tmp = headPtr;
if(headPtr == tailPtr) //if only one node in the list;
headPtr = tailPtr = 0;
else headPtr = headPtr->nextPtr;
delete tmp;
}
void linkedList::deleteNode()
{
int lineNum;
cout << "Which line number would you like to delete?: " << endl;
cin >> lineNum;
cin.ignore();
linkedListNode *pred, *tmp;
int i;
pred = headPtr;
tmp = headPtr->nextPtr;
for(i = 1; i < lineNum; i++)
{
pred = pred->nextPtr;
tmp = tmp->nextPtr;
}
if(tmp != 0)
{
pred->nextPtr = tmp->nextPtr;
if(tmp == tailPtr)
tailPtr = pred;
delete tmp;
}
}
void linkedList::deleteLines()
{
int startLine, endLine;
cout << "Which is your first desired line of deletion?: " << endl;
cin >> startLine;
cin.ignore();
cout << "\nAnd where do you wish to end?: " << endl;
cin >> endLine;
cout << endl;
linkedListNode *pred, *tmp; //start here
int i;
pred = headPtr;
tmp = headPtr->nextPtr;
for(i = 1; i < startLine; i++)
{
pred = pred->nextPtr;
tmp = tmp->nextPtr;
}
if(tmp != 0)
{
for(i = startLine; i <= endLine; i++)
{
pred->nextPtr = tmp->nextPtr;
if(tmp == tailPtr)
tailPtr = pred;
delete tmp;
}
}
}
void linkedList::addTextToLine()
{
int lineNum;
cout << "Which line would you like to append to?" << endl;
cin >> lineNum;
if(lineNum == 1)
{
addToHead();
}
char appTxt[255];
cout << "Enter the text to be appended to line " << lineNum << endl;
cin.getline(appTxt, 255);
linkedListNode *tmp;
tmp = headPtr;
int i;
for(i = 1; i <= lineNum; i++)
{
tmp = tmp->nextPtr;
}
strcat(tmp->value, appTxt);
}
void linkedList::displayList()
{
linkedListNode *temp;
int line = 1;
//Loop to display the content sof the list as long as the list has data
while (temp != NULL)
{
// Display details for what temp points to
cout << line << "> " << temp->nextPtr << " ";
line++;
cout << endl; // Line break
temp = temp->nextPtr;
}
cout << "This marks the end of the list" << endl;
}
int main()
{
int lineNum;
linkedList l;
char response;
do
{
l.displayList();
cout << endl;
cout << "Here are your options : " << endl;
cout << "E - Exit program." << endl;
cout << "I - Insert a line." << endl;
cout << "D - Delete a line." << endl;
cout << "A - Append text to a line." << endl;
cout << endl << " >> ";
cin >> response;
cin.ignore();
if(toupper(response) == 'E')
{
exit(1);
}
else if(toupper(response) == 'I')
{
cout << "OK, enter the line number before which you would like " << endl;
cout << " to insert text: " << endl;
cin >> lineNum;
if(lineNum == 1)
{
l.addToHead();
}
else if(lineNum == 0)
{
cout << "Line 1 or higher, please!" << endl;
}
else
{
l.insertLine(lineNum);
}
}
else if(toupper(response) == 'D')
{
char numberLines;
cout << "Would you like to delete one or more than on line(s)? " << endl;
cout << "A for 1, B for more than one: ";
cin >> numberLines;
cin.ignore();
here:
if(toupper(numberLines) == 'A')
{
l.deleteNode();
}
else if(toupper(numberLines) == 'B')
{
l.deleteLines();
}
else
{
cout << "Incorrect input! " << endl;
goto here;
}
}
else if(toupper(response == 'A'))
{
l.addTextToLine();
}
else
{
cout << "Sorry, that was incorrect input. Please follow the instructions " << endl;
cout << " below carefully!" << endl;
}
}
while (response != 0);
}