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 :confused:
Any suggestions are appreciated.

#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;

    }

 

}

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.

Member Avatar for Siersan

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:

while (inputfile >> PartNumber) {
  Index = HashAddress(PartNumber, Algorithm); 
  array[Index] = PartNumber;
}

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:

while (inputfile >> PartNumber) {
  Index = HashAddress(PartNumber, Algorithm); 
  array[Index] = PartNumber;
}

if (!inputfile.eof()) {
  // Handle a real input error
}

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.

Member Avatar for Siersan

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.

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 :confused:
Any suggestions are appreciated.

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

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;

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:

#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:

ifstream infile("C:\\Users\\Dave\\Documents\\test.txt");

if(!infile)
{
     cout << "a\Error!  File could not be opened!";
     exit(1);
}

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.