Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8864317
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T16:12:44+00:00 2026-06-14T16:12:44+00:00

I have created a hash class for an assignment in school that implements hashing

  • 0

I have created a hash class for an assignment in school that implements hashing and takes care of collisions. When testing this class by uploading 25 items (student records), I noticed that my hash class is simply inserting each item into the array used for storage in order(rather then using the hash code). In debug mode however the insertItemCollision() function calculates position correctly but the item is inserted in sequential order none the less.

What is causing this to happen and how can it be fixed? Thanks!

hash.h

//hash.h
#include <string>
struct Student
{
    std::string name;
    std::string id;
};

class MyHash{
public:
    MyHash();
    int hashCode(int, int);
    void insertItemCollision(std::string, std::string);
    std::string retrieveItem(std::string);
    Student students[100];
};

hash.cpp

//hash.cpp
#include <iostream>
#include "Hash.h"


MyHash::MyHash()
{
}

int MyHash::hashCode(int id, int max)
{

  return (id % max);
}

void MyHash::insertItemCollision(std::string id, std::string name)
{
    int idInt = atoi(id.c_str());
    int location;


  location = hashCode(idInt, 100);
  while (students[location].id != "")
    location = (location + 1) % 100;
  students[location].id = id;
  students[location].name = name;
}


std::string MyHash::retrieveItem(std::string id)
{
  int location;
  int startLoc;
  int idInt = atoi(id.c_str());
  bool moreToSearch = true;
  bool found;
  std::string item;

  startLoc = hashCode(idInt, 100);
  location = startLoc;
  do
  {
      if (students[location].id == id || students[location].id == "")
      moreToSearch = false;
    else
      location = (location + 1) % 100;
  } while (location != startLoc && moreToSearch);
  found = (students[location].id == id);
  if (found)
      item = students[location].name;
  return item;
}

students.txt

//students.txt
9892 Zack Lewis
4592 Ken Rodriguez
9819 Anderson Clark
1519 Ben Robinson
4597 Abigail Martinez
8542 Madison Garcia
6113 Mia Thompson
8591 Chloe Martin
9491 Daniel Harris
1698 Aiden White
5984 Alexander Walker
6541 Ethan Jackson
9549 Michael Thomas
5949 Emily Anderson
9861 Ava Taylor
5412 Noah Moore
6262 Olivia Wilson
1954 Jayden Miller
4954 William Davis
9567 Emma Brown
5195 Mason Jones
9195 Isabella Williams
5199 Sophia Johnson
1294 Jacob Smith

driver.cpp

//driver.cpp
#include <iostream>
#include<string>
#include<fstream>
#include "Hash.h"
using namespace std;


int read(string[]);
void splitString(string, Student&);
void init(string[], MyHash*, int);
int showMenu();

int main()
{
    int size;
    int choice;
    string input[100];
    MyHash* h = new MyHash();
    size = read(input); 
    init(input, h, size);

    do
    {
        choice = showMenu();
        if (choice == 1)
        {
            string id;
            cout << "Enter the id of the sutdent you would like to find: " << endl;
            cin >> id;
            std::string s = (*h).retrieveItem(id);
            if (s != "")
                cout << "The students name is: "<< s << endl;
            else
                cout << "No students matching that id was found!" <<endl;
        }

    }while (choice != 2);

    system("pause");
    return 0;
}

int read(string st[])
{
    int size = 0;
    ifstream infilestream;
    infilestream.open("test.txt");


    for(int i = 0; infilestream.good(); i++)
    {
        getline(infilestream, st[i]);
        cout<<st[i] <<endl;
        size++;
    }
    infilestream.close();
    return size;
}

void splitString(string record, Student& s)
{
    s.id = record.substr(0, 4);
    s.name = record.substr(5, record.length());
}

void init(string inputs[], MyHash* myHash, int size)
{
    for(int i = 0;i < size; i++)
    {
        splitString(inputs[i],myHash->students[i]);
        //cout << stus[i].name << " " << stus[i].id << endl;
        myHash->insertItemCollision(myHash->students[i].id, myHash->students[i].name);
    }
}

int showMenu()
{
    int chs;
    cout << "1. Find student by id." << endl;
    cout << "2. Exit." << endl;
    cin >> chs;
    return chs;
}

Fix:
These two functions were updated:

Student* splitString(string record)
{
    Student* stu = new Student();
    stu->id = record.substr(0, 4);
    stu->name = record.substr(5, record.length());
    return stu;
}

void init(string inputs[], MyHash* myHash, int size)
{
    for(int i = 0;i < size; i++)
    {
        Student* s = new Student();
        s = splitString(inputs[i]);//,myHash->students[i]);
        //cout << stus[i].name << " " << stus[i].id << endl;
        //myHash->insertItemCollision(myHash->students[i].id, myHash->students[i].name);
        myHash->insertItemCollision(s->id, s->name);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T16:12:45+00:00Added an answer on June 14, 2026 at 4:12 pm

    The problem is that you are using the member students when you insert (insertItemCollision) but also when you split (splitString). So they get overwritten.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm at a loss here. I have this class I've created that takes arguments
I have a java class that contains a hash map as a member. This
I have created one user_form class that extends zend form, it has 4 elements
I have created a method that takes in a string value, matches it with
I have created some JQuery that will expand a div 'popup' on hover and
I have created an android application that calls (using kSOAP library) a SOAP based
I have created a function that shows/hides different messages according to a combination of
I have a ghashtable object as member of my class. I have created new
I have done I custom class method for NSString to md5 a NSString. This
I have an class definition with a __hash__ function that uses the object properties

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.