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;
}