CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: hashing help

Threaded View

  1. #5
    Join Date
    May 2004
    Posts
    39
    thanks a lot!

    When I insert a Student S with age 4 and ID 5, and by the way I set the Student ID as the key.

    Main Code:
    Code:
    int key =0, insertSuccess =0, retrieveSuccess;
    
    Student S;
    Student Answer;
    
    Index<Student, int> I;
    
    S.setAge(4);
    
    S.setID(5);
    
    insertSuccess = I.Insert(S);
    
    cout << "Insert success = " << insertSuccess<< endl;
    
    retrieveSuccess = I.Retrieve(5,Answer);
    
    cout << "Retrieve success = " << retrieveSuccess<< endl;
    
    cout << "Answer.ID = " << Answer.getID << endl;
    cout << "Answer.Age = " << Answer.getAge << endl;
    Student Class:
    Code:
    #ifndef STUDENT_HPP
    #define STUDENT_HPP
    
    class Student
    {
    
    private:
    
    	// data members
    	int itsID;
    	int itsNumber;
    	char itsName[20];
    	int itsAge;
    	char itsCollege;
    
    
    public:
    	// constructor
    	Student ();
    
    	// destructor
    	~Student ();
    
    	int Hash (int key);
    	int Key () const;
    	void setID (int id);
    	int getID ();
    	void setStudent(char name[]);
    	int getStudent();
    
    	void setNumber(int number);
    	int getNumber();
    	void setAge(int age);
    	int getAge();
    	void setCollege(char college);
    	char getCollege();
    
    	bool operator != (const Student &S) const
    	{
    		if (itsID != S.itsID)
    			return true;
    		else
    			return false;
    	}
    };
    
    #endif
    Code:
    #include "student.h"
    #include <iostream.h>
    
    Student::Student ()
    {
    }
    
    // Student destructor
    Student::~Student ()
    {
    }
    
    int Student::Hash (int key)
    {
    	return key;
    }
    
    int Student::Key () const
    {
    	return itsID;
    }
    
    
    void Student::setID (int id)
    {
    	itsID = id;
    }
    
    int Student::getID ()
    {
    	return itsID;
    }
    
    void Student::setNumber(int number)
    {
    	itsNumber = number;
    }
    
    int Student::getNumber()
    {
    	return itsNumber;
    }
    
    void Student:: setAge(int age)
    {
    	itsAge = age;
    }
    
    int Student::getAge()
    {
    	return itsAge;
    }
    
    void Student::setCollege(char college)
    {
    	itsCollege = college;
    }
    
    char Student::getCollege()
    {
    	return itsCollege;
    }
    I think my insert works fine, but I could be wrong. I am trying to check if it inserted correctly and that is why I am trying to retrieve the Student class. When I try to retrieve it using a key that is not 5 it gives me 0 that it did not retrieve it, which should be correct because the key should be 5. Only when I put 5 in:

    retrieveSuccess = I.Retrieve(5,Answer);

    it gives me the answer 1 for retrieveSuccess that it retrieved that key since I set the ID to 5. Now the problem is that when I put the Student class into Answer, and I use Answer.getID and Answer.getAge it does not display the ID to 5 and Age to 4 but ID as 1 and Age as 1. Anybody can help me? Thanks in advance.
    Last edited by saiz66; June 26th, 2004 at 01:14 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured