Hey guys, I'm having some difficulties with this program I've been working on. Its composed of a few parts but I'm just focused on creating a linkedlist for this class.

I'm trying to create seperate linkedLists for each class I'm making (just focused on teachersList) and I've been getting some runtime errors. It's not entering data properly into the linked list and I'm really having trouble trying to understand why its not working. Any help is appreciated.

#include <iostream>
using namespace std;

//creates a teacher class that will be used in a linked list
class teachersList{
private:
	teachersList *next;
	char tID[5];
	char teacherName[15];
public: 
	//NULL constructor
	teachersList() 
	{
		next = NULL;
		cout << "created null teacher";
	}

	//constructor w/ data
	teachersList(char inTeacherName[15], char inTeacherID[5])
	{
		cout << "gets to add";
		if(next == NULL)  
		{
			cout << "gets  past if";
			strcpy(teacherName, inTeacherName);
			strcpy(tID, inTeacherID);
			cout << "add new first record";
		}
		else
		{
			//loop to get to end which equals null
			teachersList *current = next;
			while(current != NULL)
			{
				current= current->next;				
			}
			current->next = new teachersList(strcpy(teacherName, inTeacherName), strcpy(tID, inTeacherID));
		}

	}

	void printTeacherInfo(){
		cout << teacherName << "\t\t\t" << tID << endl;
	}
};

	//adds a new teacher to the teachers' linked list 
	void addNewTeacher(){
	char teachName[15], TID[5];
	cout << "Please input teachers name. ";
	cin >> teachName;
	cout << "Please input teachers ID (XXXX): ";
	cin >> TID;

	//store into teacher linked list
	teachersList(teachName, TID);
	cout << "finished adding new link";
}

//Displays menu and returns the choice picked by the user
int getMenuChoice(){
	int choice = 0;
	while(choice < 1 || choice > 7){
		cout << "1. Insert a new teacher" << endl;
		cout << "2. Print the teacher list" << endl;
		cout << "3. Insert a new course" << endl;
		cout << "4. Print the course list" << endl;
		cout << "5. Insert a new assignment" << endl;
		cout << "6. Print all course assignments" << endl;
		cout << "7. Exit program" << endl;
		cin >> choice;

		if(choice < 1 || choice > 7){
			cout << "Invalid choice" << endl;
		}
	}
	return choice;
}



int main(){
	int sizeOfTeachers = 0, numberOfCourses = 0, numberOfAssignments = 0, choice = 0;

	teachersList *teacher = new teachersList(); 

	while(choice != 7){

		choice = getMenuChoice();

		//new teacher
		if(choice == 1){
			addNewTeacher();
			sizeOfTeachers++;
		}

		//print teacher list
		if(choice == 2){
			cout << "2" << endl;
		}

		//insert a new course
		if(choice == 3){
			cout << "3" << endl;
		}

		//print course list
		if(choice == 4){
			cout << "4" << endl;
		}

		//insert assignment
		if(choice == 5){
			cout << "5" << endl;
		}

		//print all assignments
		if(choice == 6){
			cout << "6" << endl;
		}

		//exit program
		if(choice == 7){
			cout << "Goodbye!" << endl;
		}
	}
	return 0;
}

If you don't have to reinvent the wheel I would suggest using the std::list. For your code that you provided though line 37 looks wrong. what happens if you change it to

current->next = new teachersList(inTeacherName, inTeacherID);

Still doesn't work and I need to create the objects myself. I'm just not sure what the issues are as to why its not feeding in the data properly.

I don't see an insert method. In you add teacher function you create a teacher list with no name. The constructor you have which takes in data should actually be the insert function.

In line 19, the function prototype as... teachersList(char inTeacherName[15], char inTeacherID[5]) shouldn't be correct. Shouldn't what you need is teachersList(char[] inTeacherName, char[] inTeacherID)???

In line 22, you attempt to compare "next" pointer with null value -- if(next == NULL). I doubt that it is true unless you create an empty node first. The reason is that your "next" variable is not initialized when you declare the pointer. Not sure that C++ automatically assigns "null" value to a newly declared pointer???

In line 25, 26, I cannot remember whether strcopy() function does when the incoming string length is not matched to the current variable string. Hopefully, it will automatically cut the exceeding string...

In line 33, you check while(current != NULL) is incorrect. The result of while loop will give the "current" pointer to be "null"! As a result, Then you try to assign the new pointer to "next" of a null value???

Overall, you are not really doing it the right way. What you need is a variable to hold a linked list head. Then any other node will be added to the node. What you are doing now is just to create a node each time. Then each time a node is added, you change the head pointer to the last existing node. It will not become a linked list but a node...

1) Do you need to put new objects at the end? Since you're using a singly linked list, the simplest/fastest way to add an object to the list is to put it at the beginning:

this->next = first;  // The next of my new node points to the one that was first
first = this;        // Now my new node is the first

If you need to put it at the end, then it's more complex, because 'first' might be null because there are no items in the list yet. Here's one approach:

// Keep looping until the next node is not null but also make sure
// endScan isn't null at the beginning
teachersList *endScan;
for (endScan = first; endScan && endScan->next; endScan = endScan->next)
{
     // Do nothing but keep moving endScan to the next node
}
if (!endScan)
{
    // This is the first node, first was null
    first = this;
}
else
{
    // endScan points to the last node
    // point the 'next' pointer of the last node to my new node
    endScan->next = this;
}

2) The pointer to the beginning of the list (I'm calling it 'first' above) needs to be outside the class. Where are you keeping track of the beginning of the list? One approach is to pass a pointer to the 'first' pointer to the constructor. This would involve pointers to pointers, which usually horrifies new C programmers. C++ programmers have it easy with references: Try taking a teachersList *&first parameter in your constructor. When you create the first item, pass the pointer you want to use to track the first item in the list.

3) You are calling the constructor from inside the constructor. Look carefully at the flow of the program and see that the constructor calls itself forever (until it crashes from stack overflow) at line 37.

4) Your constructor should be taking a pointer to the string as pointers (specifically: char *), not arrays of characters. Although your taking the array of characters is not utterly wrong, that's not how you should be doing it. Your constructor should be copying the string to the classes member using strncpy and ensuring the classes buffer is null terminated, to ensure that you don't try to put too many characters into the buffer (or you could do strlen to check the lengths and throw an exception if either of the parameters are too long)

Edited 5 Years Ago by doug65536: n/a

I noticed more bugs. You have a constructor with no parameters, and you have one with parameters. Either one or the other will be called. The constructor at line 19 reads the 'next' pointer when nobody has set it yet. It will contain garbage.

This article has been dead for over six months. Start a new discussion instead.