First, thanks to Daved for helping me out with my atoi(char) problem yesterday. Now, the problem I have is this (I still have a lot of perfecting/coding to do, but this is my current sticking point): we have to create a linked list, and use it with a simple line editor, which displays lines of text, and the user must be able to choose between inserting, deleting, and appending to lines of text. I pass the line number from the display() in int main(), and it passes to insertLine() fine, but before I inserted the tmp = new linkedListNode; line, I was getting 'uninstantiated object' errors upon execution (would compile), but now, since I've added tmp = new linkedListNode; I get the error: 'linkedListNode: no default constructor available'. Code below, any help/advice would be greatly appreciated. Sorry if this seems noobish, but I really feel as if the first two C++ classes did not prepare us at all for this. Code below and attached. Thanks in advance!
-Patrick
Code:#include <iostream> #include <string> #include <cstring> #include <cstdlib> using namespace std; class linkedListNode { public: char value[255]; linkedListNode *nextPtr; linkedListNode(char val[255], linkedListNode *nPtr) { strcpy(value, val); nextPtr = nPtr; } }; 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(int); void deleteLines(int, int); void addTextToLine(char*); 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; tmp = new linkedListNode; cout << "OK, enter the text to be added to line " << num << endl; cin.getline(tmp->value, 255); int i; if(num == 1) { headPtr = tailPtr = tmp; } else { for(i = 1; i < num; i++) { tmp = tmp->nextPtr; } } secondTmp->nextPtr = tmp->nextPtr->nextPtr; tmp->nextPtr = secondTmp; //Now how the heck to insert a node????? } void linkedList::deleteFromHead() { char *p = headPtr->value; linkedListNode *tmp = headPtr; if(headPtr == tailPtr) //if only one node in the list; headPtr = tailPtr = 0; else headPtr = headPtr->nextPtr; delete tmp; } void linkedList::deleteFromTail() { char *p = tailPtr->value; if(headPtr == tailPtr) //if only one in list; { delete headPtr; headPtr = tailPtr = 0; } else { linkedListNode *tmp; for(tmp = headPtr; tmp->nextPtr != tailPtr; tmp = tmp->nextPtr); delete tailPtr; tailPtr = tmp; tailPtr->nextPtr = 0; } } void linkedList::deleteNode(int num) { linkedListNode *pred, *tmp; int i; for(i = 1; i < num; i++) { pred = pred->nextPtr; tmp = tmp->nextPtr; } if(tmp != 0) { pred->nextPtr = tmp->nextPtr; if(tmp == tailPtr) tailPtr = pred; delete tmp; } } void linkedList::addTextToLine(char *p) { atoi(p); int i; char appTxt[255]; cout << "Enter the text to be appended to line " << p << endl; cin.getline(appTxt, 255); cin.ignore(); linkedListNode *tmp; for(i = 1; i <= p[2]; i++) { tmp = tmp->nextPtr; } //strcat(tmp.value, *p); //this to access the current node.value??? } 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[255]; 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 that line number." << endl; cout << endl << " >> "; cin.getline(response, 255); if(toupper(response[0]) == 'E') { exit(1); } else if(toupper(response[0]) == '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[0]) == 'D' && isdigit(response[4])) { //l.deleteLines(response[2], response[4]); //Implement this function!!!!!!!!!!!!!!! } else if(toupper(response[0]) == 'D' && (response[2]) == '1') { l.deleteFromHead(); } else if(toupper(response[0]) == 'D' && isdigit(response[2]) && !isdigit(response[4])) { l.deleteNode(response[2]); } else if(toupper(response[0] == 'A')) { l.addTextToLine(response); } else { cout << "Sorry, that was incorrect input. Please follow the instructions " << endl; cout << " below carefully!" << endl; } } while (response != 0); }



LinkBack URL
About LinkBacks


