Ive written this program yet have stumbled upon a problem. in this program, im supposed to make a linked list of students. i must then implement some basic functions. one is to add a student (which i believe is working properly) and another is to getStudent. i believe the code is written correctly, but i may be mistaken. when i try to getStudent, i have no display on the command line. is there something wrong with my code?
any help would be greatly appreciated.
thanks!

#include <iostream>
#include <string>


using namespace std;


//new class for studentnode
class studentnode
{
public:
       string data;
       studentnode * next;  
	   

	   // string to hold student name
	   string student;

};


//new class for studentlist
class studentlist
{
private:      
      studentnode * start;
      
      
public:
       studentlist()
       {
           start = NULL;            
       }
       
   
       
       // function to add a new student to list
       void add(string student)
       {
           //create a new node
           studentnode * somenode;
           somenode = new studentnode;
           
           //put student in the new node!
           somenode->data = student;     
           
           //put new node at front of list!
           somenode->next = start;
           start = somenode;
       }
       

       //function to a specified student, if not traverse the list until found.
	   void getStudent (string student)
	   {
		   studentnode *firstNodePointer; 
		   studentnode *previousNodePointer;
		   studentnode *head;
		   head=start;

		   if(!head)
			   return;

		   if (head->data == student)
		   {
			   firstNodePointer = head;
			   head = head->next;
			   cout << student << "is here!" << endl;
		   }
		   else
		   {
			   firstNodePointer = head;

			   while (firstNodePointer != NULL && firstNodePointer->data != student)
			   {
				   previousNodePointer = firstNodePointer;
				   firstNodePointer = firstNodePointer->next;

				   if (firstNodePointer)
					   previousNodePointer->next = firstNodePointer->next;
				   delete firstNodePointer;
			   }
		   }
	   }


       

       //display all the items in the list
       void print()
       {
            studentnode * current;
            current = start;
            
           //loop through all nodes, each time doing something
           while(current != NULL )
           {
               //display the data of the current node
               cout << current->data << endl;
               
               //move current to next node
               current = current->next;               
           }     
       }
};







//main program thats .
int main()
{
    cout <<"This linked list should display students as they are added" << endl;
    
    
    studentlist listofstudents;
    
    listofstudents.add("Greg");
    listofstudents.add("James");
    listofstudents.add("Carlos");
    listofstudents.add("Bryan");
    listofstudents.add("Hector");
    
 
    
    //listofstudents.print();
    //displays all names on the list
    
    listofstudents.print();
       cout << "Did it work? :)" << endl;
    
   listofstudents.getStudent("Bryan");
  
    
    while(1){}
    return 0;
}

first of all,
instead of

while(1){}

write

cin.get();

:)

Second, can't see a destructor that deletes all the memory you've new'd.

Third, getStudent can be much shorter, like this

void getStudent (string student)
{ 
   node* current = start;
   while(current!=NULL)
   {
      if(current->name == student)
      {
         cout << "Student Found";
         break;
      }
      current = current->next;
   }
}

Hope this helped

Sorry, as I understand, get student also removes him/her from the list, right? In this case it'll be like this:

void getStudent (string student)
{ 
   node* current = start;
   node* prev = null;
   while(current!=NULL)
   {
      if(current->name == student)
      {
         cout << "Student Found"; << endl;
         if(prev)
            prev->next = current->next;
         else
            start = current->next;
         delete current;
         cout << "Student Removed" << endl;
         break;
      }
      prev = current;
      current = current->next;
  }
}

Thanks alot LordNemrod. i got it working now.
you were right the first time. i didnt want to delete the student once it was found.
i just wanted it to display when needed. i was able to get it working.

My pleasure, yeah, and, if you want to be able to track multiple entries of a certain student, just remove the break; statement :)

keeping this same code in mind, in my add function

void add(string student)
       {
           //create a new node
           studentnode * somenode;
           somenode = new studentnode;
           
           //put student in the new node!
           somenode->data = student;     
           
           //put new node at front of list!
           somenode->next = start;
           start = somenode;
       }

how can i add no just the name of the student, but an id and university name as well? this is the second part of my assignment and it seems rather confusing.

I would do it like this:
define a struct/class StudentInfo, which will hold name, address university and anything you need. Refactor your studentnode class so that it keeps a pointer to rhe next AND an instance of StudentInfo. When adding a student, pass a StudentInfo as a parameter. When "getting" a student, pass just the name and instead of
if(current->name == student)
check
if(current->Info.name == studentname)

Hope this helped, will be glad to answer any remaining questions

i completley understand the last part of the reply. the only thing im uncertain about is when you note to refactor the studentnode class so that it keeps an instance of StudentInfo. by keeping an instance, do you mean using the & operator to keep track of the adress of StudentInfo?

no, no, no! Absolutely not.
I mean like this:

struct StudentInfo
{
   string name;
   int age;
   blah blah blah
};


class studentnode
{
public:
       StudentInfo Info; // this is the instance :)
       studentnode * next;  
  
};

oh my! was i completley lost on that one!
thank you so much! i think i know what you mean now!
i really appreciate the help.

This question has already been answered. Start a new discussion instead.