my input file looks like this:

112;12
130;30
156;56

the first colum refers to keys, and the other one refers to quantity. im writin a program to search for the key and print out the quantity using hash algorithm. i used structure. so it looks like, lets say,

input.getline(structure.key, sizeof(structure.key),';');
input.getline(structure.quantity, sizeof(structure.quantity));

the problem is its just reading only the first line. and it doesnt read the rest lines at all. wat m i doing wrong???

the rest is the program if u wanna read, its too long.. sorry

//#include <fstream.h>
#include <iomanip>
#include <stdlib.h>
#include <string>


struct  hashlist
{
char part [5];
char quantity[3];
};


const int MAX_SIZE = 16;


void buildList (hashlist partList[], int& last);
void searchList (hashlist *partList, int last);
int hashKey(char *key, int last);
int collision(int last, int loc);


int main (void)
{


hashlist partList[MAX_SIZE];
int last;


cout << "HASING ALGORITHM"<<endl;
cout<<"-------------------"<<endl<<endl;
last = MAX_SIZE - 1;
buildList (partList, last);
searchList(partList, last);


return 0;
}


void buildList (hashlist partList[], int& last)
{
ifstream partnumbers;
hashlist listing;
int loc;
int cntCol;
int end;
int i;


partnumbers.open ("hashlist.TXT");
if (!partnumbers)
{
cerr<<"File cant be opened!!!!"<<endl;
abort();
}
for(i=0;i<=last;i++)
partList.part[0]='\0';


while (!partnumbers.eof())
{
partnumbers.getline(listing.part, sizeof(listing.part),';');
partnumbers.getline(listing.quantity, sizeof(listing.quantity));


loc=hashKey(listing.part,last);


if(partList[loc].part[0]!='\0')
{
end=last;
cntCol=0;
while(partList[loc].part[0]!='\0'&&cntCol++<=last)


loc=collision(last,loc);


if(partList[loc].part[0]!='\0')
{
cout<<"list is full. not all numbers read";
return;
}
}
partList[loc]=listing;
}
return;
}


int hashKey(char *key, int last)
{
int addr; int i; int keyLen;
keyLen=strlen(key);
addr=0;
for(i=0;i<keyLen;i++)
if(key!=' ')
addr+=key;
return(addr % last + 1 );
}


int collision(int last, int loc)
{
return loc<last? ++loc:0;
}


void searchList (hashlist *partList, int last)
{
char schpart[5];
char more;
int maxSrch;
int loc;
int cntCol;


do
{
cout << "Enter the Part number you wish to search: ";
cin >> schpart;


loc=hashKey(schpart,last);


/*if(strcmp(schpart, partList[loc].part)!=0)
{
maxSrch=last;
while(strcmp(schpart,partList[loc].part)!=0 && cntCol++<=maxSrch)
loc=collision(last,loc);
}*/
if(strcmp(schpart, partList[loc].part)==0)
{
cout<<"Inventory Item: "<<partList[loc].part<<" ("<<loc<<")"<<endl;
cout<<"Quantity sold: "<<partList[loc].quantity<<endl;
}
else
cout<<schpart<<" not found"<<endl;


cout<<"Would you like to try another part number <Y/N>???"<<endl;
cin>>more;
}while(more=='Y'||more=='y');
}

Edited 3 Years Ago by happygeek: fixed formatting

This article has been dead for over six months. Start a new discussion instead.