Thread: Simple Linked List Error

  1. #1
    Registered User
    Join Date
    Mar 2006
    Location
    USA::Colorado
    Posts
    155

    Question Simple Linked List Error

    Whats up?

    I've been trying to teach my brother C++, however, in a recent "Linked List" application I coded, I've been getting errors (compiles fine, they're memory errors) with pointers.

    Here's my code:
    Code:
    #include <iostream>
    #include <string>
    #include <conio.h>
    
    using namespace std;
    
    class CEmployee
    {
    	public:
    
    		string name;
    		string position;
    		string phone;
    		
    		CEmployee * next;
    };
    
    
    void new_employee ( CEmployee * root, string name, string position, string phone )
    {
    	CEmployee * e;
    
    	e = root;
    
    	if ( root == NULL )
    	{
    		root = new CEmployee;
    		root->name = name;
    		root->position = position;
    		root->phone = phone;
    		root->next = NULL;
    	}
    	else
    	{
    		while ( e->next != NULL )
    			e = e->next;
    
    		e->next = new CEmployee;
    		e = e->next;
    		e->name = name;
    		e->position = position;
    		e->phone = phone;
    		e->next = NULL;
    	}
    }
    
    int main ( int argc, char * argv[] )
    {
    	CEmployee *root;
    	CEmployee *parse;
    	root = NULL;
    
    	string buffer[3];
    
    	char res = NULL;
    
    	do
    	{
    		cout << "Employee's Name: ";
    		getline ( cin, buffer[0] );
    
    		cout << "Employee's Position: ";
    		getline ( cin, buffer[1] );
    
    		cout << "Employee's Phone Number: ";
    		getline ( cin, buffer[2] );
    
    		new_employee ( root, buffer[0], buffer[1], buffer[2] );
    
    
    		cout << "Add another employee [Y/n]? " <<endl << endl;
    		res = toupper ( _getch() );
    	} while ( res == 'Y' );
    
    	system("cls");
    
    	cout << "List of employees: " << endl << endl;
    	parse = root;
    
    	while ( parse->next != NULL )
    	{
    		cout << "Details for " << parse->name << ":" << endl <<
    				"\tName: " << parse->name << endl <<
    				"\tPosition: " << parse->position << endl <<
    				"\tPhone: " << parse->phone << endl << endl;
    		parse = parse->next;
    	}
    
    	system("pause");
    	
    
    	return 0;
    }
    What happens is at the line "parse = root," windows tells me that the program has done something wrong and now it must be debugged. What have I done wrong in this program?

    Thanks in advance for any help,

    Guitarist809
    ~guitarist809~

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > new_employee ( root, buffer[0], buffer[1], buffer[2] );
    If you want the root = new ... inside the function to affect the variable in main, then you should pass a reference to it.

    Had you debugged your code, you would have seen it go through the if ( root == NULL ) on every iteration.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM
  4. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM
  5. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 05:44 PM