Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I\'m trying to write the code for a C++ heap program. But I\'m getting the follo

ID: 3760496 • Letter: I

Question

I'm trying to write the code for a C++ heap program. But I'm getting the following error from the compiler...

_CRTIMP2_PURE __declspec(noreturn) void __CLRCALL_PURE_OR_CDECL _Xout_of_range(_In_z_ const char * _Message)
   { // report an out_of_range error
   _THROW_NCEE(out_of_range, _Message);
   }

My code is as follows...

#include "stdafx.h"
#include <iostream>
#include <vector>
#include <fstream>
#include <string>

using namespace std;

void run();

int main()
{
   run();

   system("pause");
  
   return 0;
}

int hashTable(string a)
{
   unsigned long long int key = 7;

   for(int i = 0; i < a.length(); i++)
   {
       key = ((key * 31) + a[i]);
   }
   return key % 31;
}

void run()
{
   ifstream infile;
   infile.open("input.txt");
   ofstream outfile;
   outfile.open("results.txt");

   string names[1000], number[1000];
   int i = 0;

   while(!infile.eof())
   {
       string temp;

       getline(infile , temp, '-');
       int j = temp.length();
       names[i] = temp.substr(0,(j - 4));
       number[i] = temp.substr(j - 3 , 3);
       number[i] += "-";
       getline(infile, temp);
       number[i] += temp;
       i++;
   }

   int size = i;

   infile.close();
  
   /* count collisions */

   vector< pair<string, string> > hash[31];
   int visited[31] = {0};
   int collisions = 0;

   for(int i = 0 ; i < size; i++)
   {
       int l = hashTable(names[i]);
     
       if(visited[l] == 1)
       {
           collisions++;
           hash[l].push_back(make_pair(names[i],number[i]));
       }
       else
       {
           hash[l].push_back(make_pair(names[i],number[i]));
           visited[l] = 1;
       }

   }

   cout << " Hash Table: ";
   outfile << " Hash Table: ";

   for(int i = 0 ; i < 31; i++)
   {
       for(unsigned int k = 0; k < hash[i].size(); k++)
       {
           cout << hash[i][k].first <<" | "<< hash[i][k].second;
           outfile << hash[i][k].first <<" | "<< hash[i][k].second;
       }
       cout << endl << endl;
       outfile << endl << endl;
   }

   cout << "The number of collisions are: " << collisions << endl << endl;
   outfile << "The number of collisions are: " << collisions << endl << endl;

   /* search for a name */

   string name;
  
   cout << " Enter a name to search for: ";
   outfile << " Enter a name to search for: ";
   getline(cin, name);
  
   int flag = 0;
   int m = hashTable(name);
  
   for(int i = 0 ; i < hash[m].size(); i++)
   {
       if(hash[m][i].first == name)
       {
           cout << name << " : "<< hash[m][i].second << endl << endl;
           outfile << name << " : "<< hash[m][i].second << endl << endl;
           flag = 1;
       }
   }

   if(flag == 0)
   {
       cout << name << " is not found. . . ";
       outfile << name << " is not found. . . ";
   }

   outfile.close();
}

And basically what I'm trying to accomplish is what is listed in the following... (any help or code would be much appreciated--I also don't know a ton about vectors...just trying it out)

Hash Program

Two keys: Name or Phone Nbrs

You are to write a program to set up a data base for a phone index. The key will be a person’s name and the table will supply the phone number. The data base must be implemented as a hash table with the names as the key. The input data file is out on blackboard. The format will be a person name (multiple parts) followed by a phone number. Example:   John Doe   601-5433   or O E Vay 921-1234. Only one name one phone number will be on an input line.

Set your hash table up to be 31 entries, hash table is 31 in size.

Print out the final data base (hash table) and indicate how many collisions were made in building the data base.

After building the Phone Index, show examples of looking up a name and returning a phone number.

Now I do have some folks (police usually) that have a phone number but need to know the person. So you will create a second hash key capability to use the phone number (hashed) as a key to the data base. Now you ca not create a new hash table since it is anticipated that the table will become quite large and there is only room for one copy. You can though create an index table to access the data base.

     3.     Print out the data base (names and phone numbers (hash table)) in the order of the phone hash keys.

       4.     Now test your phone number look up and show that it works again return the name and phone number.

Explanation / Answer

#include <iostream>
#include <vector>
#include <fstream>
#include <string>
using namespace std;
void run();
int main()
{
run();
//system("pause");
  
return 0;
}
int hashTable(string a)
{
unsigned long long int key = 7;

for(int i = 0; i < a.length(); i++)
{
key = ((key * 31) + a[i]);
}
return key % 31;
}
void run()
{
ifstream infile;
infile.open("input.txt");
ofstream outfile;
outfile.open("results.txt");
string names[1000], number[1000];
int i = 0;

while(!infile.eof())
{
string temp;
//cout<<"1 ";
getline(infile , temp, '-');
//cout<<"2 ";
int j = temp.length();
//cout<<"HAI ";
names[i] = temp.substr(0,(j - 4));
number[i] = temp.substr(j - 3 , 2);
//cout<<"Bye ";
number[i] += "-";
getline(infile, temp);
number[i] += temp;
i++;
}
int size = i;

infile.close();
  
/* count collisions */
vector< pair<string, string> > hash[31];
int visited[31] = {0};
int collisions = 0;
for(int i = 0 ; i < size; i++)
{
int l = hashTable(names[i]);

if(visited[l] == 1)
{
collisions++;
hash[l].push_back(make_pair(names[i],number[i]));
}
else
{
hash[l].push_back(make_pair(names[i],number[i]));
visited[l] = 1;
}
}
cout << " Hash Table: ";
outfile << " Hash Table: ";

for(int i = 0 ; i < 31; i++)
{
for(unsigned int k = 0; k < hash[i].size(); k++)
{
cout << hash[i][k].first <<" | "<< hash[i][k].second;
outfile << hash[i][k].first <<" | "<< hash[i][k].second;
}
cout << endl << endl;
outfile << endl << endl;
}
cout << "The number of collisions are: " << collisions << endl << endl;
outfile << "The number of collisions are: " << collisions << endl << endl;
/* search for a name */
string name;
  
cout << " Enter a name to search for: ";
outfile << " Enter a name to search for: ";
getline(cin, name);
  
int flag = 0;
int m = hashTable(name);
  
for(int i = 0 ; i < hash[m].size(); i++)
{
if(hash[m][i].first == name)
{
cout << name << " : "<< hash[m][i].second << endl << endl;
outfile << name << " : "<< hash[m][i].second << endl << endl;
flag = 1;
}
}
if(flag == 0)
{
cout << name << " is not found. . . ";
outfile << name << " is not found. . . ";
}
outfile.close();
}

this code is not giving any compilation error

i have executed my code here

http://www.tutorialspoint.com/compile_cpp_online.php