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:
// 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
{
friendclass 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
// source.cpp
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include "student.h"
usingnamespace 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.