UPDATED....New issue with addstudent function.
I made some changes based on feedback. I also made an update to the addstudent()function based on the assignment requirements and I am running into a strange issue.
The requirements for the addstudent function are that the parameters be a string id, string name, and string major. I added a line that allocates a new student node in this function, but it seems that only the ID is being kept. When I print the student information, the ID shows up, but the name and major are both blank. Where am I going wrong?
#include <iostream>
#include <string.h>
using namespace std;
class Student
{
public:
string ID;
string name;
string major;
public:
Student *next;
Student()
{
ID = "0";
name = "0";
major = "0";
next = NULL;
}
Student(string id, string name, string major)
{
ID = id;
name = name;
major = major;
next = NULL;
}
};
class Classroom
{
public:
Student* head;
Classroom()
{
head = NULL;
}
Classroom(Student* n)
{
head = n;
}
void addStudent(string id, string name, string major)
{
Student *n=new Student(id, name, major);
Student* current;
if (head == NULL)
{
n->next = head;
head = n;
}
else if(head->name > n->name)
{
n->next = head;
head = n;
}
else
{
current = head;
while (current->next!=NULL &&
current->next->name < n->name)
{
current = current->next;
}
n->next = current->next;
current->next = n;
}
}
void removeStudent(string id)
{
if (!head)
{
cout << "No students exist in this list.\n";
return;
}
Student **precurrent = &head,
*current = head;
while (current)
{
if (current->ID == id)
{
*precurrent = current->next;
cout<< "Student removed from list.\n";
return;
}
precurrent = ¤t->next;
current = current->next;
}
cout << "No student exists with this ID.\n";
}
void print()
{
if (!head) {
cout << "No students in list.\n";
return;
}
for (Student *temp = head; temp; temp = temp->next)
cout <<"\nStudent Information:\n Student ID: " << temp->ID
<< "\n Student Name: " << temp->name
<< "\n Student Major: " << temp->major << '\n';
}
void print(string id)
{
if (!head)
{
cout << "No students in list.\n";
return;
}
for (Student *temp = head; temp; temp = temp->next)
{
if (temp->ID == id)
{
cout <<"Student Information: \n"<< "Student ID: " << temp->ID <<'\n' << "Student Name: " << temp->name <<'\n' << "Student Major: " << temp->major << '\n';
return;
}
if(temp==NULL)
{
cout<<"Student ID does not exist.\n";
return;
}
}
}
bool isEmpty()
{
if (head == NULL)
return true;
else;
return false;
}
int getSize()
{
int count = 0;
if(head==NULL)
{
return count;
}
Student* temp = head;
while (temp != NULL)
{
temp = temp->next;
count++;
}
return count;
}
Student *at(int index)
{
Student *temp = head;
for (int i=1; i < index && temp; i++, temp = temp->next) {}
if (!temp)
cout << "Index not found.\n";
return temp;
}
};
int main()
{
Classroom cs;
int choice;
do
{
cout <<"\nPlease select an option below: (Enter 0 to exit)\n"
<< "1. Add student.\n"
<< "2. Remove student.\n"
<< "3. Print all student.\n"
<< "4. Print specific student.\n"
<< "5. Print student at index.\n"
<< "6. Print number of students.\n"
<< "7. Check if student list is empty.\n\n";
cin >> choice;
Student* s1 = new Student();
switch (choice)
{
case 0:
break;
case 1:
{
string id, name, major;
cout << "Enter Student ID:";
cin.ignore();
getline(cin, id);
cout << "Enter Student Name:";
getline(cin, name);
cout << "Enter Student Major:";
getline(cin, major);
cs.addStudent(id, name, major);
break;
}
case 2:
{
string id;
cout << "Enter Student ID of the Student to be removed: ";
cin >> id;
cs.removeStudent(id);
break;
}
case 3:
cs.print();
break;
case 4:
{
string id;
cout << "Enter ID of student to print: ";
cin >> id;
cs.print(id);
break;
}
case 5:
{
int index;
if(cs.isEmpty())
{
cout<< "No Students Exists.\n";
break;
}
cout << "Enter index to print: ";
cin >> index;
Student *s=cs.at(index);
if(s)
cs.print(s->ID);
break;
}
case 6:
cout << "There are "<< cs.getSize() << " students.\n";
break;
case 7:
if(cs.isEmpty())
cout<< "No Students Exists.\n";
else
cout << "The list is not empty.\n";
break;
default:
cout << "Enter valid option (0-7)\n";
}
} while (choice != 0);
return 0;
}