| | |
Reading txt file into Hash Table
![]() |
•
•
Join Date: Jan 2005
Posts: 5
Reputation:
Solved Threads: 0
I have this program that is supposed to read part numbers from a text file.
101-110, 301-310, 501-510 and put them into a hash table. It will prompt for one of four algorithms to be used and then continue to load the part numbers. Everything seems to be working okay, but it seems to repeat the last part number 510. Is there something that I need to put at the end of the text file to indicate it is the end of file and to stop. Please be nice - I am just a beginner - had to do quite a bit of reading to get this far
Any suggestions are appreciated.
101-110, 301-310, 501-510 and put them into a hash table. It will prompt for one of four algorithms to be used and then continue to load the part numbers. Everything seems to be working okay, but it seems to repeat the last part number 510. Is there something that I need to put at the end of the text file to indicate it is the end of file and to stop. Please be nice - I am just a beginner - had to do quite a bit of reading to get this far
Any suggestions are appreciated.
C++ Syntax (Toggle Plain Text)
#include <iostream.h> #include <fstream.h> #include <stdio.h> //function declaration int HashAddress (int PartNumber, char Algorithm); // global variables int array[41]; int main() { int i = 0; int max_read = 41; int amountRead = 0; int PartNumber, Index; char Algorithm; // set array elements to -1 for(i = 0; i < 41; i++) { array[i] = -1; } std::ifstream inputfile("part.txt",std::ios::in |std::ios::binary); if(!inputfile) { std::cout<<"Could not open file"<<std::endl; return 1; } // prompt for input from user to choose algorithm cout<<"Please select Algorithm\n"; cout<<"1. Modulo Division using linear probing \n"; cout<<"2. Modulo Division using key offset \n"; cout<<"3. PsuedoRandom using linear probing \n"; cout<<"4. Rotation using linear probing \n"; cin >> Algorithm; //this is where we are reading in the information into our array for(i = 0; i < 41; i++) { // as you read each part number in the file inputfile >> PartNumber; // HashAddress function returns array index and it is assigned to variable Index Index = HashAddress(PartNumber,Algorithm); // put part number in array array[Index] = PartNumber; } return 0; } // function HashAddress returns location for hash table int HashAddress(int PartNumber, char Algorithm) { int Index, count; int x, y, z; // use algorithms to hash address switch(Algorithm) { case '1': // first algorithm, Modulo Division with linear probing Index = PartNumber % 41; break; case '2': // second algorithm, Modulo Division with key offset Index = PartNumber % 41; break; case '3': // third algorithm, Psuedorandom with linear probing Index = (17 * PartNumber + 7) % 41; break; case '4': // fourth algorithm, Rotation with linear probing x = PartNumber % 10; y = PartNumber / 10; z = x * 10; Index = (z+y) % 41; break; default: cout << "Invalid Selection\n"; //invalid choice } cout << "\nPart Number: " << " " << PartNumber << " " << "Index: " << Index; // check to see if array element is occupied if(array[Index] < 0) // array element is available so return the address/location return Index; else //collision, part number is already in that spot { count = 0; // use count to stop if table is full do { switch(Algorithm) { case '1': Index = (Index + 1) % 41; // % 41 allows for wrap around break; case '2': Index = (PartNumber / 41) % 41; case '3': Index = (Index + 1) % 41; // % 41 allows for wrap around break; case '4': Index = (Index + 1) % 41; // % 41 allows for wrap around break; default: cout << "Invalid Selection\n"; //invalid choice } count ++; } while ((array[Index] > 0) && (count < 41)); if (count == 41) { //print ERROR hash table is full cout << "Hash table is full. Can not insert part number."; } else cout << "\nCollision count:" << " " << count << " "<< "New Index:" << " " << Index; return Index; } }
The best way to read from a file is to use the input gathering function itself as your loop condition. That way when it returns a failure code (supposedly for reaching end-of-file), you break the loop:
I removed your comments because they do not add anything. Comments that say the same thing as the code being commented only serve to make the program harder to read. There's also a good chance that any changes to the code will not be reflected in the comment and the two will disagree. When code and comments disagree, it's customary to see both as incorrect.
After the loop, it's also a good idea to make sure that it was end-of-file that caused it to terminate. That way you can handle real errors:
C++ Syntax (Toggle Plain Text)
while (inputfile >> PartNumber) { Index = HashAddress(PartNumber, Algorithm); array[Index] = PartNumber; }
After the loop, it's also a good idea to make sure that it was end-of-file that caused it to terminate. That way you can handle real errors:
C++ Syntax (Toggle Plain Text)
while (inputfile >> PartNumber) { Index = HashAddress(PartNumber, Algorithm); array[Index] = PartNumber; } if (!inputfile.eof()) { // Handle a real input error }
•
•
Join Date: Jan 2005
Posts: 5
Reputation:
Solved Threads: 0
thanks - I was thinking about it after - and I am only entering 30 part numbers - and the array holds 41 elements...I know its not the best way to do it...but I just changed the value to 30. I think there must be a fancy way to put something in there to know its at the end of the file. But I will worry about that another time.
•
•
Join Date: Aug 2006
Posts: 72
Reputation:
Solved Threads: 0
•
•
•
•
I have this program that is supposed to read part numbers from a text file.
101-110, 301-310, 501-510 and put them into a hash table. It will prompt for one of four algorithms to be used and then continue to load the part numbers. Everything seems to be working okay, but it seems to repeat the last part number 510. Is there something that I need to put at the end of the text file to indicate it is the end of file and to stop. Please be nice - I am just a beginner - had to do quite a bit of reading to get this far![]()
Any suggestions are appreciated.
C++ Syntax (Toggle Plain Text)
#include <iostream.h> #include <fstream.h> #include <stdio.h> //function declaration int HashAddress (int PartNumber, char Algorithm); // global variables int array[41]; int main() { int i = 0; int max_read = 41; int amountRead = 0; int PartNumber, Index; char Algorithm; // set array elements to -1 for(i = 0; i < 41; i++) { array[i] = -1; } std::ifstream inputfile("part.txt",std::ios::in |std::ios::binary); if(!inputfile) { std::cout<<"Could not open file"<<std::endl; return 1; } // prompt for input from user to choose algorithm cout<<"Please select Algorithm\n"; cout<<"1. Modulo Division using linear probing \n"; cout<<"2. Modulo Division using key offset \n"; cout<<"3. PsuedoRandom using linear probing \n"; cout<<"4. Rotation using linear probing \n"; cin >> Algorithm; //this is where we are reading in the information into our array for(i = 0; i < 41; i++) { // as you read each part number in the file inputfile >> PartNumber; // HashAddress function returns array index and it is assigned to variable Index Index = HashAddress(PartNumber,Algorithm); // put part number in array array[Index] = PartNumber; } return 0; } // function HashAddress returns location for hash table int HashAddress(int PartNumber, char Algorithm) { int Index, count; int x, y, z; // use algorithms to hash address switch(Algorithm) { case '1': // first algorithm, Modulo Division with linear probing Index = PartNumber % 41; break; case '2': // second algorithm, Modulo Division with key offset Index = PartNumber % 41; break; case '3': // third algorithm, Psuedorandom with linear probing Index = (17 * PartNumber + 7) % 41; break; case '4': // fourth algorithm, Rotation with linear probing x = PartNumber % 10; y = PartNumber / 10; z = x * 10; Index = (z+y) % 41; break; default: cout << "Invalid Selection\n"; //invalid choice } cout << "\nPart Number: " << " " << PartNumber << " " << "Index: " << Index; // check to see if array element is occupied if(array[Index] < 0) // array element is available so return the address/location return Index; else //collision, part number is already in that spot { count = 0; // use count to stop if table is full do { switch(Algorithm) { case '1': Index = (Index + 1) % 41; // % 41 allows for wrap around break; case '2': Index = (PartNumber / 41) % 41; case '3': Index = (Index + 1) % 41; // % 41 allows for wrap around break; case '4': Index = (Index + 1) % 41; // % 41 allows for wrap around break; default: cout << "Invalid Selection\n"; //invalid choice } count ++; } while ((array[Index] > 0) && (count < 41)); if (count == 41) { //print ERROR hash table is full cout << "Hash table is full. Can not insert part number."; } else cout << "\nCollision count:" << " " << count << " "<< "New Index:" << " " << Index; return Index; } }
This is because when you are reading the numbers from the file you are trying to read 41 characters..but only 31 characters are present in the file.. so it is reading the last cahracter 10 more times.Try to decrease the value of i in the for loop and you will get your desired result.
![]() |
Similar Threads
- Segmentation Fault Reading File Into Hash Table (C++)
- reading a txt file into a 2d array (C++)
- Conversion of CSV file to hash table (Perl)
- reading txt file into array (C++)
Other Threads in the C++ Forum
- Previous Thread: Help? reading from one file to another...
- Next Thread: connecting client to server
| Thread Tools | Search this Thread |
api array based binary bitmap business c++ c/c++ char class classes code coding commentinghelp compile console conversion count decide delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez graph guess gui homeworkhelp homeworkhelper iamthwee ifpug ifstream incrementoperators infinite input int integer java lib linkedlist linker loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem proficiency program programming project python random read recursion reference rpg string strings temperature template templates test text text-file tree url variable vector video win32 windows winsock word wordfrequency wxwidgets







As I said before, the best way is to use the return value of your input function as a loop controller.