943,507 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 15147
  • C++ RSS
Feb 20th, 2005
0

Reading txt file into Hash Table

Expand 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.

C++ Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
LisaJane is offline Offline
5 posts
since Jan 2005
Feb 20th, 2005
0

Re: Reading txt file into Hash Table

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.
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Feb 20th, 2005
0

Re: Reading txt file into Hash Table

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:
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 12
Solved Threads: 2
Light Poster
Siersan is offline Offline
45 posts
since Jan 2005
Feb 20th, 2005
0

Re: Reading txt file into Hash Table

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
LisaJane is offline Offline
5 posts
since Jan 2005
Feb 20th, 2005
0

Re: Reading txt file into Hash Table

Quote ...
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.
Reputation Points: 12
Solved Threads: 2
Light Poster
Siersan is offline Offline
45 posts
since Jan 2005
Oct 8th, 2008
0

Re: Reading txt file into Hash Table

Click to Expand / Collapse  Quote originally posted by LisaJane ...
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)
  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.
Reputation Points: 45
Solved Threads: 1
Junior Poster in Training
rati is offline Offline
78 posts
since Aug 2006
Nov 24th, 2009
0
Re: Reading txt file into Hash Table
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; Nov 24th, 2009 at 5:04 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
xaveri is offline Offline
7 posts
since Nov 2009
Nov 24th, 2009
0
Re: Reading txt file into Hash Table
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:
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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; Nov 24th, 2009 at 5:22 pm. Reason: because I got a fever, and the only prescription is more cowbell.
Reputation Points: 237
Solved Threads: 117
Practically a Posting Shark
Clinton Portis is offline Offline
822 posts
since Oct 2005
Nov 27th, 2009
0
Re: Reading txt file into Hash Table
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
xaveri is offline Offline
7 posts
since Nov 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Compile Freezes (Says assignment is not working and to end task)
Next Thread in C++ Forum Timeline: Project, need help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC