Thread: Pointers

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    46

    Question Pointers

    I'm building a simple student database with pointers and basically a queue. I'm having trouble putting it together as I'd like to keep track of their name and ID..Here's my code thus far..

    Code:
    class studentList{
    public:
      int id;
      string name;
      studentList *next;
    };
    
    int main(){
      
      studentList *temp = NULL;
      studentList *front = NULL;
      studentList *back = NULL;
      int id, option;
      string name;
      
      do{
        
        cout<<"1. Add New Student"<<endl;
        cout<<"2. Delete First Student"<<endl;
        cout<<"3. List All Students"<<endl;
        cout<<"4. Exit"<<endl;
        cout<<"Option: ";
        cin>>option;
    
        if(option == 1){
          
          temp = new studentList;
          int _id;
          string _name;
          cout<<"Please Enter Student's Name"<<endl;;
          cin>>temp->name;
          cout<<"Please Enter Student's ID"<<endl;;
          cin>>temp->id;      
          
        }
        else if(option == 2 && front != NULL){
          temp = front;
          front = front->next;
          delete temp;
        }
        
        else if(option == 3){
          temp = front;
          while(temp){
    	cout<<temp->name<<endl;
    	temp = temp->next;
          }
          
        }
        else if(option == 4){
          cout<<"Thanks For Using This Program!!"<<endl;
        }
        else{
          cout<<"Invalid Option"<<endl;
        }
      }while(option != 4);
        
      return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    Define "having trouble". What does that mean ? We can't just guess what you don't understand.

    Edit: But you may want to use a switch() instead of multiple if() / else() / else if().

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    46
    Well with my enqueue and dequeue aka add and delete students I can't figure out how you can keep track of the student and the id with the same queue then show it.

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    46
    So I figured out how to make my queue hold the name and show it.. but how can I get it to keep the ID with the name in the queue?

    Code:
        if(option == 1){
    
          cout<<"Please Enter Student's Name"<<endl;;
          cin>>name;
          // cout<<"Please Enter Student's ID"<<endl;;
          //cin>>id;
          temp = new studentList;
          temp->name = name;
          if(front == NULL){
            front = temp;
            back = front;
          }
          else{
            back->next = temp;
            back = temp;
          }
        }

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    46
    Actually I just figured it out! ( : ..amazing how you can really have a brain meltdown and forget everything!!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Variable pointers and function pointers
    By Luciferek in forum C++ Programming
    Replies: 11
    Last Post: 08-02-2008, 02:04 AM
  2. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM