View Single Post
Old 01-12-2005, 12:31 AM   #2 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Quote:
And after that, how do I find info on a student, do I have to transverse the course linked list to find all the info I need?
What do you need exactly?
- Do you have to show all the students info (names, phone AND grades and classes).
- Or do you have to display a very specific students info?

The latter can't be done with linked lists. They are not designed for random access. However, you could traverse the list, but only show the desired student, for examply by its first name or ID. Compare ID's from student class with ID in grades.dat for example.

Do you also want to add new students to the dat file? Or new courses to the course file? Etcetera.

Anyway, start with thinkin about the world you are talking about. This is called domain problem analysis.
Here is my view on what a student is:
Code:
#ifndef STUDENT_H
#define STUDENT_H

#include <string>

using std::string;

struct Student
{
  string _FirstName;
  string _LastName;
  string _ID;
  double _PhoneNumber;
};

#endif //STUDENT_H
And so on with other classes.

Then I was experimenting with a StudentLinkedList:
Code:
#ifndef STUDENTLINKEDLIST_H
#define STUDENTLINKEDLIST_H

#include "Student.h"
#include <iostream>

using std::cout;
using std::endl;

struct Node
{
  Student* Item;
  Node *Next;
};

class StudentLinkedList
{
public:
  void AddItem(Student* itm);
  void ShowItems();
  StudentLinkedList() : m_nodeStart(0){ }
  ~StudentLinkedList();
private:
  Node* m_nodeStart;
};

void StudentLinkedList::AddItem(Student* itm)
{
  Node* p = m_nodeStart;
  m_nodeStart =  new Node;
  m_nodeStart->Next = p;
  m_nodeStart->Item = itm;
}

void StudentLinkedList::ShowItems()
{
  for(Node* p =  m_nodeStart; p != 0; p = p->Next)
    cout<<p->Item->_FirstName << endl;
}

StudentLinkedList::~StudentLinkedList()
{
  Node* p;
  while(m_nodeStart != 0)
  {
    p = m_nodeStart;
    m_nodeStart = m_nodeStart->Next;
    delete p;
  }
}

#endif //STUDENTLINKEDLIST_H
This list could be generalized (templates) if needed. Depends on if you need more Linked lists. You aren't very clear about it.

Then here is a quick test:
Code:
#include <iostream>
#include <string>
#include <climits>
#include "Student.h"
#include "StudentLinkedList.h"

using std::cin;
using std::numeric_limits;
using std::streamsize;
using std::string;

//Optional function: depends on IDE. Remove if not needed.
void wait_for_enter();

int main( int argc, char *argv[] )
{
  Student aStudent;
  aStudent._FirstName = "Alex";
  
  StudentLinkedList theStudents;
  theStudents.AddItem(&aStudent);
  theStudents.ShowItems();

  
  wait_for_enter();
  return 0;
}

//---------------------------------------------------

void wait_for_enter()
{
  cout << "press <enter> to continue...";
  // Reset failstate, just in case.
  cin.clear();
  string line;
  getline( cin, line);
}
So basically these are my very first thoughts. From here I could build the things I need, if I only new exactly what I need. See if this starter helps.

Anyway, the questions that remain are:
- Do you want to modify info? If so, what exactly?
- Do you have to build a linked list of courses and grades as well? If so, do you have to traverse these lists as well for some reason?

Etcetera etcetera. If you're stuck, then post the requirements more accurately next time you post. This way I will know how to analyse things and how to setup a design exactly.

Your current skillset is also relevant. If you mention "menu driven" then I can think of a few designs. But some may be too much right now. That's why.

Good luck!
__________________

Last edited by Valmont; 01-12-2005 at 01:05 AM.
Valmont is offline   Reply With Quote