Thread: Help with C++, linked lists

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    1

    Help with C++, linked lists

    Hi guys,

    I'm trying to write a program that stores a name, ID number, average test score, and letter grade for up to 20 students. This code here will only store/print one student's information. I can't figure out how to get it to store more than that. Any advice?

    Code:
    #include <iostream>
    using namespace std;
    
    struct node
      {  char name[20];		 // Name of up to 20 letters
         int idNum;       
         int avgScore[20];     
    	 char grade[20];
         node *nxt;			 // Pointer to next node
      };
    
    node *start_ptr = NULL;
    node *current;		 // Used to move along the list
    int option = 0;
    
    void add_record()
      {  node *temp, *temp2;   // Temporary pointers
    	 //node avgScore;
         // Reserve space for new node and fill it with data
    	 int* a = NULL;   // Pointer to int, initialize to nothing.
    	 int n;           // Size needed for array
    	 cout << "\n\t--------------Student Information---------------\n";
    	 cout << "How many test scores this semester? ";
    	 cin >> n;        // Read in the size
    	 a = new int[n];  // Allocate n ints and save ptr in a.
    	 for (int i=0; i<n; i++) 
    	 {
    		 a[i] = 0;    // Initialize all elements to zero.
    	 }
         
    	 int m;
    	 cout << "How many students in the course? ";
    	 cin >> m;
    	 temp = new node;
    	 for (int k=0; k<m; k++)
    	 {
    		cout << "Please enter the name of student " << (k+1) << ": ";
    		cin >> temp->name;
    		cout << "Please enter the ID Number of the student : ";
    		cin >> temp->idNum;
    		cout << "Please enter student's test scores for the " << n << " tests: ";
    		for (int j=0; j<n; j++)
    		{
    			cout << "\nTest " << (j+1) << ": ";
    			cin >> a[j];
    			/*for (int p=0; p<j; p++)
    			{
    				temp->avgScore = (temp->avgScore + a[p]) / p;
    			}*/
    		}
    
    	 }
    
         temp->nxt = NULL;
    
         // Set up link to this node
         if (start_ptr == NULL)
           { start_ptr = temp;
    	 current = start_ptr;
           }
         else
           { temp2 = start_ptr;
             // We know this is not NULL - list not empty!
             while (temp2->nxt != NULL)
               {  temp2 = temp2->nxt;
                  // Move to next link in chain
               }
             temp2->nxt = temp;
           }
      }
    
    void display_list()
      {  node *temp;
         temp = start_ptr;
         cout << endl;
         if (temp == NULL)
           cout << "List currently empty." << endl;
         else
           { while (temp != NULL)
    	   {  // Display details for what temp points to
                  cout << "Name : " << temp->name << " ";
                  cout << "idNum : " << temp->idNum << " ";
    			  cout << "avgScore : " << temp->avgScore;
    	      if (temp == current)
    		cout << " <-- Current node";
                  cout << endl;
    	      temp = temp->nxt;
    
    	   }
    	 cout << "End of list!" << endl;
           }
      }
    
    void main()
      {  start_ptr = NULL;
    
         do
    	{
    	  display_list();
    	  cout << endl;
    	  cout << "Please select an option : " << endl;
    	  
    	  cout << "1. Create new record." << endl;
    	  cout << "2. Exit the program." << endl;
    
    	  cin >> option;
    
    	  switch (option)
    	    {
    	      case 1 : add_record(); break;
    	    }
    	}
         while (option != 2);
      }
    I know not all the steps are there for everything, my main concern is how to get it to store/print more than one student. I appreciate any responses.

    Thanks!

  2. #2
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    Well it seems you're struct Node shouldn't contain arrays for the values.

    You should have a Node like this:

    Code:
    struct Node{
    
      string name;
      int idNum;       
      int avgScore;     
      char grade;
      Node *nxt;
    };
    Then you want to make a list of these structs.

    And get rid of the global pointers to the struct ( actually you don't need global variables at all ).


    Also consider initializing all of node's attributes in a constructor like this:
    Code:
    struct Node{
    
      string name;
      int id;
      int avg;
      char grade;
      Node* nxt;
    
      // Constructor initialization list:
      Node( string n, int i, int a, char g, Node* nx = 0 )
      : name( n ), id( i ), avg( a ), grade( g ), nxt( nx ) {}
    
    };
    This will make your code a lot clearer when you write the rest of your funcitons.
    Last edited by dudeomanodude; 05-20-2008 at 07:18 PM.
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Singly Linked Lists: Clarification Needed
    By jedispy in forum C++ Programming
    Replies: 4
    Last Post: 12-14-2006, 05:30 PM
  2. Linked Lists 101
    By The Brain in forum C++ Programming
    Replies: 5
    Last Post: 07-24-2004, 04:32 PM
  3. Map file formats and linked lists
    By Spitball in forum Game Programming
    Replies: 2
    Last Post: 03-04-2004, 11:32 PM
  4. need help w/ linked lists
    By MKashlev in forum C++ Programming
    Replies: 11
    Last Post: 08-05-2002, 08:57 PM
  5. doubly linked lists
    By qwertiop in forum C++ Programming
    Replies: 3
    Last Post: 10-03-2001, 06:25 PM