//main.cpp #include "code.h" 1 int main (void){ 2 3 List *obj = new List(); 4 char *input = new char[20]; 5 int option = 0; 6 bool caseBool = true; 7 8 while (caseBool != false){ 9 10 cout << "1. Enter contact: " << endl; 11 cout << "2. Print Contacts" << endl; 12 cout << "3. Exit" << endl; 13 cin >> option; 14 15 system ("cls"); 16 17 if (option == 1){ 18 19 cout << "Enter Contact Name: "; 20 cin >> input; <--------------------This line changes the contents of the whole list 21 obj->insertFront(input); to the second value I enter. 22 } 23 else if (option == 2){ 24 25 obj->print(); 26 system ("pause"); 27 } else if (option == 3){ caseBool = false; } else{ system ("cls"); cout << "Invalid input" << endl; system ("pause"); } system ("cls"); } return 0; } //code.cpp #include "code.h" void List::insertFront(char* data){ ListNode *newNode = new ListNode(data); if (isEmpty()){ newNode->nextPtr = NULL; firstPtr = newNode; } else{ newNode->nextPtr = firstPtr; firstPtr = newNode; } } void List::print() const{ ListNode *tempPtr = firstPtr; while (tempPtr != NULL){ cout << tempPtr->data << endl; tempPtr = tempPtr->nextPtr; } } bool List::isEmpty(){ bool empty = false; if (firstPtr == NULL){ return true; } return empty; } // code.h #pragma once #include <iostream> using namespace std; class ListNode{ friend class List; public: //explicit ListNode(int initialVal) //: data(initialVal){} explicit ListNode(char* name) : data(name){}; //ListNode (const ListNode &rhs); char *getData()const{ return data; }; void writeData(char* input){ data = input; } private: char *data; ListNode *nextPtr; }; class List{ public: List(){ firstPtr = NULL; }; ~List(){ delete firstPtr; }; void insertFront (char *data); bool isEmpty(); void print() const; private: ListNode *firstPtr; };
Line 20 is the line that is making the entire list into the second word I enter. How can I fix this I am stumped.