Using contents of a linked list in multiple places

Hey guys, I'm having trouble with one aspect of a project I'm working. I'm designing a grade book application that in part lets a user enter in the names of students in their class and stores those names in a linked list. Then, if they want, under another menu item, they can view the class list. I'm not having any issues allowing the user to enter in student's names, but when I try to get those names to print in another section of the program, it just prints that no student's names have been entered. So, with that said, here's my latest failed attempt to get this to work. Thanks in advance to anyone who looks over my 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
// studentNode.h
#ifndef STUDENTNODE_H
#define STUDENTNODE_H

// forward declaration of class Student and class displayNames
// so that it can be used in a friend declaration.
template< typename NODETYPE > class Student;

template< typename NODETYPE >
class StudentNode
{
	friend class Student< NODETYPE >; // make Student a friend

public:
	StudentNode( const NODETYPE & ); // constructor
	NODETYPE getNames() const; // return names in node
private:
	NODETYPE names; // names
	StudentNode< NODETYPE > *nextPtr; // next node in list
}; // end class ListNode

// constructor
template< typename NODETYPE >
StudentNode< NODETYPE >::StudentNode( const NODETYPE &info )
	: names( info ), nextPtr( 0 )
{
	// empty body
} // end StudentNode constructor

// return copy of data in node
template< typename NODETYPE >
NODETYPE StudentNode< NODETYPE >::getNames() const
{
	return names;
} // end function getNames

#endif 


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
// student.h
#ifndef STUDENT_H
#define STUDENT_H

#include <iostream>
#include "studentNode.h"
using namespace std;

template< typename NODETYPE >
class Student
{
public:
	Student(); // constructor
	~Student(); // destructor
	void getNames( const NODETYPE & );
	bool isEmpty() const;
	void print() const;
private:
	StudentNode< NODETYPE > *firstPtr; // pointer to first node
	StudentNode< NODETYPE > *lastPtr; // pointer to last node

	// utility function to allocate new student nodes
	StudentNode< NODETYPE > *getNewStudent( const NODETYPE & );
}; // end class Student

// default construcot
template< typename NODETYPE >
Student< NODETYPE >::Student()
	: firstPtr( 0 ), lastPtr( 0 )
{
	// empty body
} // end Student constructor

// destructor
template< typename NODETYPE >
Student< NODETYPE >::~Student()
{
	if ( !isEmpty() ) // Student is not empty
	{
		cout << "Destroying nodes...\n";

		StudentNode< NODETYPE > *currentPtr = firstPtr;
		StudentNode< NODETYPE > *tempPtr;

		while ( currentPtr != 0 ) // delete remaining nodes
		{
			tempPtr = currentPtr;
			cout << tempPtr->names << '\n';
			currentPtr = currentPtr->nextPtr;
			delete tempPtr;
		} // end while
	} // end if

	cout << "All nodes Destroyed\n\n";
} // end Student destructor

// insert a student's name to the front of the list
// student names will be sorted later alphabetically
template< typename NODETYPE >
void Student< NODETYPE >::getNames( const NODETYPE &newNames )
{
	StudentNode< NODETYPE > *newPtr = getNewStudent( newNames ); // new node

	if ( isEmpty() ) // Student is empty
		firstPtr = lastPtr = newPtr; // new list has only one node
	else // Student is not empty
	{
		newPtr->nextPtr = firstPtr; // point new node to previous 1st node
		firstPtr = newPtr; // aim firstPtr at new node
	} // end else
} // end function getNames

template< typename NODETYPE >
bool Student< NODETYPE >::isEmpty() const
{
	return firstPtr == 0;
} // end function isEmpty

// return pointer to newly allocated student node
template< typename NODETYPE >
StudentNode< NODETYPE > *Student< NODETYPE >::getNewStudent( const NODETYPE &newNames )
{
	return new StudentNode< NODETYPE >( newNames );
} // end function getNames

// display student's names
template< typename NODETYPE >
void Student< NODETYPE >::print() const
{
	if ( isEmpty() ) // Student is empty
	{
		cout << "No students' names have been entered yet.\n\n";
		return;
	} // end if

	StudentNode< NODETYPE > *currentPtr = firstPtr;

	cout << "Gradebook:\n\n ";

	while ( currentPtr != 0 ) // get element data
	{
		cout << currentPtr->names << '\n';
		currentPtr = currentPtr->nextPtr;
	} // end while
} // end function print

#endif 


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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// source.cpp
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include "student.h"
using namespace std;

void mainMenu();
int classList();

enum Choices { LIST = 1, GRADES, CLASS, STUDENT, GROUP, EXIT };
enum classChoices { VIEW = 1, ADD, MAIN };

// function to get names of students in a class
template< typename T >
void studentList( Student< T > &studentObject )
{
	cout << "How many students are in your class?: ";
	int classAmount;
	T newName; // store input value
	int count = 0;
	cin >> classAmount;
	do
	{
		cout << "Enter a student's name: ";
		cin >> newName;
		studentObject.getNames( newName );
		++count;
	} while ( classAmount > count ); // end do...while

	system( "pause" );
	system( "cls" );
}// end function studentList

// function to print names of students in a class
template< typename T >
void displayNames( Student< T > &studentObject )
{
	studentObject.print();
	system( "pause" );
	system( "cls" );
} // end function displayNames

int main()
{
	cout << "\n WELCOME TO TEACH ASSIST\n\n An Andrew Leary Software Production\n" 
           << endl;
	system( "pause" );
	system( "cls" );

	mainMenu();

} // end main

// Main Menu
void mainMenu()
{
	cout << "Main Menu:\n\n"
		<< "1 - Class List" << endl
		<< "2 - Enter Grades" << endl
		<< "3 - Class Analysis" << endl
		<< "4 - Student Analysis" << endl
		<< "5 - Group Project Creator" << endl
		<< "6 - Exit" << endl;

	cout << "\n\nWhere would you like to go?" << endl;

	int menuChoice; // store user choice
	int classChoice;
	int menuItem;

	cin >> menuItem; // input menu selection from user

	// enable user to specify action
	while ( menuChoice = menuItem )
	{
		switch ( menuChoice )
		{
		case LIST:
			system( "cls" );
			while ( classChoice = classList() )
			{
				switch ( classChoice )
				{
				case VIEW:
				{
					system( "cls" );
					Student< string > stringList;
					displayNames( stringList );
				} // end case VIEW
				case ADD:
				{
					system( "cls" );
					Student< string > stringStudent;
					studentList( stringStudent );
				} // end case ADD
				case MAIN:
					system( "cls" );
					mainMenu();
				} // end switch
			} // end while
			break;
		case GRADES:
			system( "cls" );
			cout << "Enter Grades" << endl;
			break;
		case CLASS:
			system( "cls" );
			cout << "Class Analysis" << endl;
			break;
		case STUDENT:
			system( "cls" );
			cout << "Student Analysis" << endl;
		case GROUP:
			system( "cls" );
			cout << "Create Groups" << endl;
			break;
		default: // display error if user does not select valid choice
			cerr << "Incorrect choice" << endl;
			break;
		} // end switch
	} // end while
} // end function mainMenu

int classList()
{
	cout << "Class List:\n\n"
		<< "1 - View Class List" << endl
		<< "2 - Add Students" << endl
		<< "3 - Main Menu" << endl;
	
	int classList;
	cout << "\n\nWhere would you like to go?" << endl;
	cin >> classList;
	return classList;
} // end function clssList 

Is Student the list type you talk about? In case VIEW you create stringList but that object will stop to exist as soon as it goes out of scope on line 91. In ADD you also have the same problem with stringStudent. You create new Student objects each time and new Student objects are empty. What you want is probably to reuse the same Student object for the whole loop by declaring it outside the loop.
Peter87,

Thanks man, that's exactly what I wanted to do. Logically it makes perfect sense--guess I just needed a new set of eyes on it.

Thanks again.
Topic archived. No new replies allowed.