| | |
Reading txt file into Hash Table
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
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: 78
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.
•
•
Join Date: Nov 2009
Posts: 2
Reputation:
Solved Threads: 0
0
#7 20 Days Ago
new to c++
i just need to know what does this line mean?
and why its not working with my compiler?
it is giving an error that
type qualifier 'std' must be a struct or class name
std::ifstream inputfile("part.txt",std::ios::in |std::ios::binary);
if(!inputfile)
{
std::cout<<"Could not open file"<<std::endl;
return 1;
i just need to know what does this line mean?
and why its not working with my compiler?
it is giving an error that
type qualifier 'std' must be a struct or class name
std::ifstream inputfile("part.txt",std::ios::in |std::ios::binary);
if(!inputfile)
{
std::cout<<"Could not open file"<<std::endl;
return 1;
Last edited by xaveri; 20 Days Ago at 5:04 pm.
0
#8 20 Days Ago
The recommended scheme for File I/O is to create an fstream object, attempt to open a file, perform error checking, and load the file.
The code in question is performing error checking to see if the file was actually opened. It is quite possible that the file may have been moved, deleted, renamed.. or was just never created:
A slight variation involves the use of the ifstream constructor:
The code in question is performing error checking to see if the file was actually opened. It is quite possible that the file may have been moved, deleted, renamed.. or was just never created:
C++ Syntax (Toggle Plain Text)
#include<fstream> //create an fstream object ifstream infile; //attempt to open a file infile.open("C:\\Users\\Dave\\Documents\\test.txt"); //perform error checking if(!infile.is_open()) { cout << "a\Error! File could not be opened!"; exit(1); }
A slight variation involves the use of the ifstream constructor:
C++ Syntax (Toggle Plain Text)
ifstream infile("C:\\Users\\Dave\\Documents\\test.txt"); if(!infile) { cout << "a\Error! File could not be opened!"; exit(1); }
Last edited by Clinton Portis; 20 Days Ago at 5:22 pm. Reason: because I got a fever, and the only prescription is more cowbell.
•
•
Join Date: Nov 2009
Posts: 2
Reputation:
Solved Threads: 0
0
#9 18 Days Ago
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
I am getting an error on this line. Isn't inputfile the part of any of the headers?
C:\Program Files\Microsoft Visual Studio\VC98\Bin\dont.cpp(52) : error C2065: 'inputfile' : undeclared identifier
C:\Program Files\Microsoft Visual Studio\VC98\Bin\dont.cpp(52) : warning C4552: '>>' : operator has no effect; expected operator with side-effect
I am getting an error on this line. Isn't inputfile the part of any of the headers?
C:\Program Files\Microsoft Visual Studio\VC98\Bin\dont.cpp(52) : error C2065: 'inputfile' : undeclared identifier
C:\Program Files\Microsoft Visual Studio\VC98\Bin\dont.cpp(52) : warning C4552: '>>' : operator has no effect; expected operator with side-effect
![]() |
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: Compile Freezes (Says assignment is not working and to end task)
- Next Thread: Project, need help
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api array arrays based beginner binary bmp c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete deploy desktop directshow dll download dynamic encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib library linkedlist linker list loop looping loops map math matrix memory microsoft newbie news number output pointer problem program programming project python random read recursion recursive reference simple string strings studio system temperature template templates test text text-file tree unix url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets







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