Hash table, need help badly.

Okay, so my assignment is to read the file "CityOut.txt", specifically the entries for the "city" and "state_id" on each line, then put those two values into a visual output.
The code I have so far looks like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
  #include <iostream>
#include <fstream>
#include<bits/stdc++.h> 
using namespace std;

class node
{
  public:
    string city;
    string state_id;
    string line;
    node *next;
};
class Hash
{
  public:
    string city;
    string state_id;
    string line;
    int BUCKET;    
  
    list<int> *table; 
    public: 
      Hash ( int H );   
  
    void insertItem ( int I ); 
        int hashFunction ( int I ) { 
        return ( I % BUCKET ); 
    } 
  
    void displayHash ( ); 
}; 
  
Hash::Hash( int B ) 
{ 
    this->BUCKET = B; 
    table = new list<int>[ BUCKET ]; 
} 
  
void Hash::insertItem ( int key ) 
{ 
    int index = hashFunction( key ); 
    table[index].push_back( key );  
}
  

void Hash::displayHash ( ) { 
  for ( int H = 0; H < BUCKET; H++ ) { 
    cout << H; 
    for ( auto I : table [ H ] ) 
      cout << " --> " << I; 

    cout << endl; 
  } 
} 

int main()
{

  string city;
  string state_id;
  string line;
  string temp = "ZZ";
  char myAlpha []="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
 

 
  ifstream myIn ("CityOut.txt");
  
  int hashIn = 0;



  while (getline(myIn, city, ',')&& getline(myIn,state_id,','))
      
  {
    getline (myIn, line);
      for (int count=0; count < sizeof (myAlpha)/sizeof (myAlpha [0]); count ++)
      {
        if (state_id.at(0) == myAlpha [count])
        {
          hashIn += count;
        }
        else if (state_id.at(1) == myAlpha [count])
        {
          break;
        }
        
        
        cout << city << city.at(0)<< hashIn <<endl;
        cout << state_id << state_id.at(0)<< hashIn <<endl;
        hashIn = 0;

}
}
}

the file I am loading from (CityOut) is this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
South Creek,WA,Washington,53053,Pierce,53053,46.9994,-122.3921,2500,125
Roslyn,WA,Washington,53037,Kittitas,53037,47.2507,-121.0989,947,84
Fenwick Island,DE,Delaware,10005,Sussex,10005,38.4603,-75.0542,429,499
Washington,DC,District of Columbia,11001,District of Columbia,11001,38.9047,-77.0163,5289420.0,4382
Stone Lake,WI,Wisconsin,55129,Washburn,55129|55113,45.8445,-91.5502,181,74
Arbovale,WV,West Virginia,54075,Pocahontas,54075,38.4353,-79.8116,137,90
Paauilo,HI,Hawaii,15001,Hawaii,15001,20.0397,-155.3696,596,221
North Key Largo,FL,Florida,12087,Monroe,12087,25.2574,-80.3242,1009,20
Whiting,WY,Wyoming,56031,Platte,56031,42.0089,-104.9711,78,25
Somersworth,NH,New Hampshire,33017,Strafford,33017,43.2534,-70.8856,11900,468
Carlstadt,NJ,New Jersey,34003,Bergen,34003,40.8247,-74.0613,6314,617
El Cerro Mission,NM,New Mexico,35061,Valencia,35061,34.7687,-106.6447,4684,307
Trenton,TX,Texas,48147,Fannin,48147,33.4275,-96.3407,646,133
Norco,LA,Louisiana,22089,St. Charles,22089,29.9986,-90.4036,3144,352
Oscarville,AK,Alaska,02050,Bethel,02050,60.7287,-161.7730,36,5
Trenton,NC,North Carolina,37103,Jones,37103,35.0640,-77.3553,285,484
Langdon,ND,North Dakota,38019,Cavalier,38019,48.7624,-98.3747,1738,433
Ansley,NE,Nebraska,31041,Custer,31041,41.2863,-99.3824,427,229
Trenton,TN,Tennessee,47053,Gibson,47053,35.9689,-88.9449,3678.0,195
Port Jervis,NY,New York,36071,Orange,36071,41.3782,-74.6909,16410.0,1317
Cheyney University,PA,Pennsylvania,42029,Chester,42029|42045,39.9343,-75.5306,557,805
Hope Valley,RI,Rhode Island,44009,Washington,44009,41.5151,-71.7200,1777,206
Lamoille,NV,Nevada,32007,Elko,32007,40.7239,-115.4790,228,47
Dale City,VA,Virginia,51153,Prince William,51153,38.6473,-77.3459,73279,1986
Sanford,CO,Colorado,08021,Conejos,08021,37.2574,-105.9007,876,230
El Dorado Hills,CA,California,06017,El Dorado,06017,38.6750,-121.0490,45104,359
Moores Mill,AL,Alabama,01089,Madison,01089,34.8491,-86.5222,6695,190
McDougal,AR,Arkansas,05021,Clay,05021,36.4365,-90.3907,171,177
North Westminster,VT,Vermont,50025,Windham,50025,43.1219,-72.4555,231,187
Trenton,IL,Illinois,17027,Clinton,17027,38.6070,-89.6844,2638.0,599
Trenton,GA,Georgia,13083,Dade,13083,34.8741,-85.5096,2766.0,268
Crawfordsville,IN,Indiana,18107,Montgomery,18107,40.0428,-86.8975,18305.0,645
Lidderdale,IA,Iowa,19027,Carroll,19027,42.1225,-94.7837,173,27
West Falmouth,MA,Massachusetts,25001,Barnstable,25001,41.6012,-70.6364,1867,233
Freedom Acres,AZ,Arizona,04007,Gila,04007,34.3199,-111.3051,113,24
Albion,ID,Idaho,16031,Cassia,16031,42.4097,-113.5805,272,222
North Grosvenor Dale,CT,Connecticut,09015,Windham,09015,41.9862,-71.9034,1086,185
Sanford,ME,Maine,23031,York,23031,43.4244,-70.7573,29264.0,169
Dargan,MD,Maryland,24043,Washington,24043,39.3767,-77.7341,141,184
Wister,OK,Oklahoma,40079,Le Flore,40079,34.9699,-94.7205,1062,208
Trenton,OH,Ohio,39017,Butler,39017,39.4791,-84.4620,12912,1093
Trenton,UT,Utah,49005,Cache,49005,41.9116,-111.9356,528,24
Trenton,MO,Missouri,29079,Grundy,29079,40.0819,-93.6030,5431.0,348
Birchwood Village,MN,Minnesota,27163,Washington,27163,45.0598,-92.9777,888,1005
Trenton,MI,Michigan,26163,Wayne,26163,42.1395,-83.1930,18255,970
Holyrood,KS,Kansas,20053,Ellsworth,20053,38.5876,-98.4120,433,388
Cooke City,MT,Montana,30067,Park,30067,45.0187,-109.9119,41,1
Crystal Springs,MS,Mississippi,28029,Copiah,28029,31.9903,-90.3540,5069.0,345
María Antonia,PR,Puerto Rico,72055,Guánica,72055,17.9789,-66.8887,1115,2108
Trenton,SC,South Carolina,45037,Edgefield,45037,33.7401,-81.8404,193,59
Trenton,KY,Kentucky,21219,Todd,21219,36.7225,-87.2628,375,255
Crawfordsville,OR,Oregon,41043,Linn,41043,44.3561,-122.8650,450,90
White Horse,SD,South Dakota,46121,Todd,46121,43.3061,-100.5948,160,19


and the final result needs to look like this:

0 --> Trenton/MO --> Trenton/UT
1 --> North Westminster/VT
2 --> Holyrood/KS --> Dargan/MD --> El Dorado Hills/CA --> Cheyney University/PA --> Trenton/NC --> Paauilo/HI
3 --> Sanford/ME --> Sanford/CO --> Langdon/ND --> Trenton/TX --> North Key Largo/FL
4 --> Crystal Springs/MS --> McDougal/AR --> Ansley/NE --> Arbovale/WV --> Stone Lake/WI
5 --> Crawfordsville/OR --> Cooke City/MT --> Washington/DC
6 --> María Antonia/PR --> Trenton/GA --> Trenton/IL --> Trenton/TN
7 --> Trenton/SC --> Trenton/MI --> Somersworth/NH --> Whiting/WY --> Fenwick Island/DE
8 --> White Horse/SD --> Trenton/KY --> Trenton/OH --> North Grosvenor Dale/CT --> Lidderdale/IA --> Crawfordsville/IN --> Dale City/VA --> Lamoille/NV
9 --> Carlstadt/NJ --> Roslyn/WA --> South Creek/WA
10 --> Oscarville/AK
11 --> Wister/OK --> Albion/ID --> Moores Mill/AL --> Port Jervis/NY --> Norco/LA
12 --> Birchwood Village/MN --> Freedom Acres/AZ --> West Falmouth/MA --> Hope Valley/RI --> El Cerro Mission/NM

my output looks... awful. I really need help guys. =(
Last edited on
this is an example of my output, unfortunately. As you can see, it looks nothing like the output it needs to have.
WVW0
ArbovaleA0
WVW0
ArbovaleA0
WVW0
ArbovaleA0
WVW0
ArbovaleA0
WVW0
ArbovaleA0
WVW0
ArbovaleA0
WVW0
ArbovaleA0
WVW0
ArbovaleA0
WVW0
ArbovaleA0
WVW0
ArbovaleA0
WVW0
ArbovaleA0
WVW0
ArbovaleA0
WVW0
ArbovaleA0
WVW0
ArbovaleA0
WVW0
ArbovaleA0
WVW0
ArbovaleA0
WVW0
PaauiloP0
HIH0
PaauiloP0
HIH0
PaauiloP0
HIH0
PaauiloP0
HIH0
PaauiloP0
HIH0
PaauiloP0
HIH0
PaauiloP0
HIH0
PaauiloP7
HIH7
North Key LargoN0
FLF0
North Key LargoN0
FLF0
North Key LargoN0
FLF0
North Key LargoN0
FLF0
North Key LargoN0
FLF0
North Key LargoN5
FLF5
North Key LargoN0
FLF0
 
I can't make sense of what you want to do. Please explain it as if we had no idea what each column in the data is, and what you want to do exactly.

for example your first output line has 2 trentons but there are like 10 in the data. How do we know which 2 are #1 ? Is this some sort of traveling salesmanish thing where you have city one travel to city 2, via some mysterious code in one of the file's columns?

The first thing i'd do is move the cout out of the for loop bc it looks like your just printing the same thing out a random number of times. Or at least put it inside the if statements whatever makes sense.
Last edited on
Sorry about the jumble. What I am trying to do is read each line in the input file "CityOut.txt", then print JUST the city and state_id (WA, OR, CA etc...) in the output. Issue is, they have to be in a hash table that looks like the one I provided.
Okay, so I moved the cout statements into the if, and the output looks better. Not right, but better.
1
2
3
4
cout << city << city.at(0)<< hashIn <<endl;
        cout << state_id << state_id.at(0)<< hashIn <<endl;
        cout << " --> ";
        hashIn = 0;  


My new output looks like this:
Fenwick IslandF3
DED3
 --> PaauiloP7
HIH7
 --> North Key LargoN5
FLF5
 --> WhitingW22
WYW22
 --> TrentonT19
TXT19
 --> OscarvilleO0
AKA0
 --> Port JervisP13
NYN13
 --> LamoilleL13
NVN13
 --> SanfordS2
COC2
 --> Moores MillM0
ALA0
 --> McDougalM0
ARA0
 --> TrentonT8
ILI8
 --> CrawfordsvilleC8
INI8
 --> Freedom AcresF0
AZA0
 --> North Grosvenor DaleN2
CTC2
 --> TrentonT12
MOM12
 --> Birchwood VillageB12
MNM12
 --> HolyroodH10
KSK10
 --> Cooke CityC12
MTM12
 --> Crystal SpringsC12
MSM12
 --> María AntoniaM15
PRP15
 --> TrentonT10
KYK10
 --> CrawfordsvilleC14
ORO14
 -->  
Topic archived. No new replies allowed.