Linked list of a student database

Mar 6, 2013 at 4:38pm
I've remade a program to store student database using linked list. Can anyone help me to find whats wrong with my code when I try to display the list of student with their information (It displays nothing)

Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <iostream>
#include <string>

using namespace std;

struct StudentRecord
{
       string firstName;
       string lastName;
       string dob;
       float gpa;
       StudentRecord *next;
};

bool processMenu() {
     
     string s_firstName;
     string s_lastName;
     string s_dob;
     float s_gpa;
     
     StudentRecord *head;
     head = NULL;
     
     StudentRecord *newStudentRecord;
     StudentRecord *StudentRecord_ptr;
     newStudentRecord = new StudentRecord;
     newStudentRecord -> firstName = s_firstName;
     newStudentRecord -> lastName = s_lastName;
     newStudentRecord -> dob = s_dob;
     newStudentRecord -> gpa = s_gpa;
     newStudentRecord -> next = NULL;
     
	       cout << endl <<"What would you like to do?" << endl;
	       cout << "=========================" << endl;
	       cout << "1. Enter a student record. " << endl;
	       cout << "2. List all student records. " << endl;
	       cout << "3. Exit program. " << endl;
	       cout << endl;
	 
	       char choice;
	       cin >> choice;
  
	       if(choice == '1'){
	                 cout << endl;
                     cout << "You chose option #1." << endl;
	                 cout << "What is the student's first name?: ";
	                 cin>> s_firstName;
					 cout << "What is the student's last name?: ";
					 cin >> s_lastName;
					 cout << "What is the student's date of birth?: ";
					 cin >> s_dob;
					 cout << "What is the student's GPA?: ";
					 cin >> s_gpa;
	                 
                     if(!head)
                       head = newStudentRecord;
                     else
                     {
                         StudentRecord_ptr = head;
        
                         while(StudentRecord_ptr -> next)
                            StudentRecord_ptr = StudentRecord_ptr -> next;
           
                            StudentRecord_ptr -> next = newStudentRecord;
                      }
	                 
	                 
	                 processMenu(); // Run processMenu() again
	                 
	                 
	       }else if(choice == '2'){
                     cout << endl;
                     cout << "You chose option #3." << endl;
	                 cout << endl << "Listing all student records: " << endl;
	                 cout << "-----------------------" << endl;
                     
					 StudentRecord *Display_ptr;
                     Display_ptr = head;
           
                     while(Display_ptr != NULL)
                     {
                        cout << Display_ptr -> firstName << endl;
                        cout << Display_ptr -> lastName << endl;
                        cout << Display_ptr -> dob << endl;
                        cout << Display_ptr -> gpa << endl;
            
                        Display_ptr = Display_ptr -> next;
                        cout << endl;
                      }
				
      		 
	                 processMenu();
	            
           }else{ 
                     return false; 
           }
	       return true;
}


int main() 
{
   cout << "Student Record Program." << endl << endl;
	 
   processMenu();	  
 
   system("pause");
   return 0;
} 


thanks
Mar 6, 2013 at 4:47pm
Your problem is you're calling processMenu recursively at lines 69 and 93.

For example, after adding the first student, you call processMenu which results in two processMenu frames on the stack. The second instance of processMenu gets new occurrances of it's local variables, specifically head which is initialied to NULL. If you now choose list, the list will appear empty.
Mar 6, 2013 at 4:53pm
What do I change it to then?
Mar 6, 2013 at 4:57pm
I change the code to:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main() 
{
   cout << "Student Record Program." << endl << endl;
	 
  	   while (!processMenu()) 
        {
     	  
              cout << "Sorry, that was an invalid choice." << endl;
	          cout << "Please try again." << endl << endl;
	    }
 
   system("pause");
   return 0;
} 


but still output nothing when I display student info
Mar 6, 2013 at 5:07pm
Do you understand recursion?
See Recursive Procedures here:
http://en.wikipedia.org/wiki/Recursion_(computer_science)

You need to change processMenu so it doesn't call itself.
As I explained before, each time processMenu cals itself, it gets a new set of local variables.

Topic archived. No new replies allowed.