1 Replies - 173 Views - Last Post: 29 January 2013 - 09:24 PM Rate Topic: -----

#1 muttfacejohnson  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 29-January 13

C++ char* problem: changing the input changes the entire linked list

Posted 29 January 2013 - 05:03 PM


//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.

Is This A Good Question/Topic? 0
  • +

Replies To: C++ char* problem: changing the input changes the entire linked list

#2 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1305
  • View blog
  • Posts: 4,490
  • Joined: 19-February 09

Re: C++ char* problem: changing the input changes the entire linked list

Posted 29 January 2013 - 09:24 PM

The pointer input points to a place in memory where the string is located. When you create a new ListNode, the pointer is copied, so data points to the same string as input.

You need to allocate some memory and copy the string. Also when the node is destroyed it should free the memory.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1