Reading txt file into Hash Table

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2005
Posts: 5
Reputation: LisaJane is an unknown quantity at this point 
Solved Threads: 0
LisaJane LisaJane is offline Offline
Newbie Poster

Reading txt file into Hash Table

 
0
  #1
Feb 20th, 2005
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.

  1. #include <iostream.h>
  2. #include <fstream.h>
  3. #include <stdio.h>
  4.  
  5.  
  6. //function declaration
  7. int HashAddress (int PartNumber, char Algorithm);
  8.  
  9. // global variables
  10. int array[41];
  11.  
  12. int main()
  13. {
  14.  
  15. int i = 0;
  16. int max_read = 41;
  17. int amountRead = 0;
  18. int PartNumber, Index;
  19. char Algorithm;
  20.  
  21. // set array elements to -1
  22. for(i = 0; i < 41; i++)
  23. {
  24.  
  25. array[i] = -1;
  26.  
  27. }
  28.  
  29. std::ifstream inputfile("part.txt",std::ios::in |std::ios::binary);
  30.  
  31. if(!inputfile)
  32. {
  33.  
  34. std::cout<<"Could not open file"<<std::endl;
  35. return 1;
  36.  
  37. }
  38. // prompt for input from user to choose algorithm
  39.  
  40. cout<<"Please select Algorithm\n";
  41. cout<<"1. Modulo Division using linear probing \n";
  42. cout<<"2. Modulo Division using key offset \n";
  43. cout<<"3. PsuedoRandom using linear probing \n";
  44. cout<<"4. Rotation using linear probing \n";
  45. cin >> Algorithm;
  46.  
  47.  
  48.  
  49. //this is where we are reading in the information into our array
  50. for(i = 0; i < 41; i++)
  51. {
  52.  
  53. // as you read each part number in the file
  54. inputfile >> PartNumber;
  55.  
  56. // HashAddress function returns array index and it is assigned to variable Index
  57. Index = HashAddress(PartNumber,Algorithm);
  58.  
  59. // put part number in array
  60. array[Index] = PartNumber;
  61.  
  62. }
  63.  
  64. return 0;
  65.  
  66. }
  67.  
  68. // function HashAddress returns location for hash table
  69. int HashAddress(int PartNumber, char Algorithm)
  70. {
  71.  
  72. int Index, count;
  73. int x, y, z;
  74.  
  75.  
  76. // use algorithms to hash address
  77. switch(Algorithm)
  78. {
  79. case '1': // first algorithm, Modulo Division with linear probing
  80. Index = PartNumber % 41;
  81.  
  82.  
  83. break;
  84.  
  85. case '2': // second algorithm, Modulo Division with key offset
  86. Index = PartNumber % 41;
  87.  
  88. break;
  89.  
  90. case '3': // third algorithm, Psuedorandom with linear probing
  91. Index = (17 * PartNumber + 7) % 41;
  92.  
  93. break;
  94.  
  95. case '4': // fourth algorithm, Rotation with linear probing
  96. x = PartNumber % 10;
  97. y = PartNumber / 10;
  98. z = x * 10;
  99. Index = (z+y) % 41;
  100.  
  101. break;
  102.  
  103. default:
  104. cout << "Invalid Selection\n"; //invalid choice
  105. }
  106.  
  107. cout << "\nPart Number: " << " " << PartNumber << " " << "Index: " << Index;
  108.  
  109. // check to see if array element is occupied
  110. if(array[Index] < 0)
  111.  
  112. // array element is available so return the address/location
  113. return Index;
  114.  
  115. else //collision, part number is already in that spot
  116. {
  117. count = 0; // use count to stop if table is full
  118.  
  119. do
  120. {
  121.  
  122. switch(Algorithm)
  123. {
  124. case '1':
  125. Index = (Index + 1) % 41; // % 41 allows for wrap around
  126. break;
  127.  
  128. case '2':
  129.  
  130. Index = (PartNumber / 41) % 41;
  131.  
  132. case '3':
  133. Index = (Index + 1) % 41; // % 41 allows for wrap around
  134. break;
  135.  
  136. case '4':
  137. Index = (Index + 1) % 41; // % 41 allows for wrap around
  138. break;
  139.  
  140. default:
  141. cout << "Invalid Selection\n"; //invalid choice
  142. }
  143. count ++;
  144. } while ((array[Index] > 0) && (count < 41));
  145.  
  146.  
  147.  
  148.  
  149. if (count == 41)
  150. {
  151. //print ERROR hash table is full
  152. cout << "Hash table is full. Can not insert part number.";
  153. }
  154. else
  155. cout << "\nCollision count:" << " " << count << " "<< "New Index:" << " " << Index;
  156.  
  157. return Index;
  158.  
  159. }
  160.  
  161.  
  162.  
  163. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,109
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 943
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Reading txt file into Hash Table

 
0
  #2
Feb 20th, 2005
I am not quite sure what your part.txt file looks like, but you have to tell your for loop when the file ends, or it will just keep going 40 times.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 45
Reputation: Siersan is an unknown quantity at this point 
Solved Threads: 2
Siersan's Avatar
Siersan Siersan is offline Offline
Speaker of Truth

Re: Reading txt file into Hash Table

 
0
  #3
Feb 20th, 2005
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:
  1. while (inputfile >> PartNumber) {
  2. Index = HashAddress(PartNumber, Algorithm);
  3. array[Index] = PartNumber;
  4. }
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:
  1. while (inputfile >> PartNumber) {
  2. Index = HashAddress(PartNumber, Algorithm);
  3. array[Index] = PartNumber;
  4. }
  5.  
  6. if (!inputfile.eof()) {
  7. // Handle a real input error
  8. }
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 5
Reputation: LisaJane is an unknown quantity at this point 
Solved Threads: 0
LisaJane LisaJane is offline Offline
Newbie Poster

Re: Reading txt file into Hash Table

 
0
  #4
Feb 20th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 45
Reputation: Siersan is an unknown quantity at this point 
Solved Threads: 2
Siersan's Avatar
Siersan Siersan is offline Offline
Speaker of Truth

Re: Reading txt file into Hash Table

 
0
  #5
Feb 20th, 2005
I think there must be a fancy way to put something in there to know its at the end of the file.
There is, but it is broken. As I said before, the best way is to use the return value of your input function as a loop controller.
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 78
Reputation: rati is an unknown quantity at this point 
Solved Threads: 0
rati rati is offline Offline
Junior Poster in Training

Re: Reading txt file into Hash Table

 
0
  #6
Oct 8th, 2008
Originally Posted by LisaJane View Post
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.

  1. #include <iostream.h>
  2. #include <fstream.h>
  3. #include <stdio.h>
  4.  
  5.  
  6. //function declaration
  7. int HashAddress (int PartNumber, char Algorithm);
  8.  
  9. // global variables
  10. int array[41];
  11.  
  12. int main()
  13. {
  14.  
  15. int i = 0;
  16. int max_read = 41;
  17. int amountRead = 0;
  18. int PartNumber, Index;
  19. char Algorithm;
  20.  
  21. // set array elements to -1
  22. for(i = 0; i < 41; i++)
  23. {
  24.  
  25. array[i] = -1;
  26.  
  27. }
  28.  
  29. std::ifstream inputfile("part.txt",std::ios::in |std::ios::binary);
  30.  
  31. if(!inputfile)
  32. {
  33.  
  34. std::cout<<"Could not open file"<<std::endl;
  35. return 1;
  36.  
  37. }
  38. // prompt for input from user to choose algorithm
  39.  
  40. cout<<"Please select Algorithm\n";
  41. cout<<"1. Modulo Division using linear probing \n";
  42. cout<<"2. Modulo Division using key offset \n";
  43. cout<<"3. PsuedoRandom using linear probing \n";
  44. cout<<"4. Rotation using linear probing \n";
  45. cin >> Algorithm;
  46.  
  47.  
  48.  
  49. //this is where we are reading in the information into our array
  50. for(i = 0; i < 41; i++)
  51. {
  52.  
  53. // as you read each part number in the file
  54. inputfile >> PartNumber;
  55.  
  56. // HashAddress function returns array index and it is assigned to variable Index
  57. Index = HashAddress(PartNumber,Algorithm);
  58.  
  59. // put part number in array
  60. array[Index] = PartNumber;
  61.  
  62. }
  63.  
  64. return 0;
  65.  
  66. }
  67.  
  68. // function HashAddress returns location for hash table
  69. int HashAddress(int PartNumber, char Algorithm)
  70. {
  71.  
  72. int Index, count;
  73. int x, y, z;
  74.  
  75.  
  76. // use algorithms to hash address
  77. switch(Algorithm)
  78. {
  79. case '1': // first algorithm, Modulo Division with linear probing
  80. Index = PartNumber % 41;
  81.  
  82.  
  83. break;
  84.  
  85. case '2': // second algorithm, Modulo Division with key offset
  86. Index = PartNumber % 41;
  87.  
  88. break;
  89.  
  90. case '3': // third algorithm, Psuedorandom with linear probing
  91. Index = (17 * PartNumber + 7) % 41;
  92.  
  93. break;
  94.  
  95. case '4': // fourth algorithm, Rotation with linear probing
  96. x = PartNumber % 10;
  97. y = PartNumber / 10;
  98. z = x * 10;
  99. Index = (z+y) % 41;
  100.  
  101. break;
  102.  
  103. default:
  104. cout << "Invalid Selection\n"; //invalid choice
  105. }
  106.  
  107. cout << "\nPart Number: " << " " << PartNumber << " " << "Index: " << Index;
  108.  
  109. // check to see if array element is occupied
  110. if(array[Index] < 0)
  111.  
  112. // array element is available so return the address/location
  113. return Index;
  114.  
  115. else //collision, part number is already in that spot
  116. {
  117. count = 0; // use count to stop if table is full
  118.  
  119. do
  120. {
  121.  
  122. switch(Algorithm)
  123. {
  124. case '1':
  125. Index = (Index + 1) % 41; // % 41 allows for wrap around
  126. break;
  127.  
  128. case '2':
  129.  
  130. Index = (PartNumber / 41) % 41;
  131.  
  132. case '3':
  133. Index = (Index + 1) % 41; // % 41 allows for wrap around
  134. break;
  135.  
  136. case '4':
  137. Index = (Index + 1) % 41; // % 41 allows for wrap around
  138. break;
  139.  
  140. default:
  141. cout << "Invalid Selection\n"; //invalid choice
  142. }
  143. count ++;
  144. } while ((array[Index] > 0) && (count < 41));
  145.  
  146.  
  147.  
  148.  
  149. if (count == 41)
  150. {
  151. //print ERROR hash table is full
  152. cout << "Hash table is full. Can not insert part number.";
  153. }
  154. else
  155. cout << "\nCollision count:" << " " << count << " "<< "New Index:" << " " << Index;
  156.  
  157. return Index;
  158.  
  159. }
  160.  
  161.  
  162.  
  163. }


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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 2
Reputation: xaveri is an unknown quantity at this point 
Solved Threads: 0
xaveri xaveri is offline Offline
Newbie Poster
 
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;
Last edited by xaveri; 20 Days Ago at 5:04 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 478
Reputation: Clinton Portis is on a distinguished road 
Solved Threads: 55
Clinton Portis's Avatar
Clinton Portis Clinton Portis is offline Offline
Posting Pro in Training
 
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:
  1. #include<fstream>
  2.  
  3. //create an fstream object
  4. ifstream infile;
  5.  
  6. //attempt to open a file
  7. infile.open("C:\\Users\\Dave\\Documents\\test.txt");
  8.  
  9. //perform error checking
  10. if(!infile.is_open())
  11. {
  12. cout << "a\Error! File could not be opened!";
  13. exit(1);
  14. }

A slight variation involves the use of the ifstream constructor:
  1. ifstream infile("C:\\Users\\Dave\\Documents\\test.txt");
  2.  
  3. if(!infile)
  4. {
  5. cout << "a\Error! File could not be opened!";
  6. exit(1);
  7. }
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 2
Reputation: xaveri is an unknown quantity at this point 
Solved Threads: 0
xaveri xaveri is offline Offline
Newbie Poster
 
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
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC