hello all, i am having a real hard time tracing down a logic problem and am looking for help. this program just does a binary search on a sorted list and returns a record. now it works for records that exist. however, i want it to continue searching until an exit code is given. that works as well. the problem is coming when i search for a record that does not exist. it will print and error, but then when i search for an existing record it tells me it does not exist.

any clues? below is the c code the data is a byte file so i cant upload it.

code:

/**************************************************************************************************
Devang N. Joshi											  *
Homework Four											  *
CSCI 325 - Binary Search									  *
March 7th, 2011										  	  *
												  *
The purpose of this program is to implement a binary search on sorted data			  *
**************************************************************************************************/
#include <ctype.h>										 //
#include <stdio.h>			//<--header files					 //
#include <stdlib.h>
#include <stdbool.h>										 //
/*************************************************************************************************/
struct Faculty_Info 										 //
{												 //
   char fname[15],lname[15];									 //
   int phone, office;			//<--data structure for faculty info			 //
   char dept[5];										 //
};												 //
/*************************************************************************************************/
void main ()											 //
{												 //
												 //
	struct Faculty_Info TEMP;	//<--declare a TEMP data structure			 //
   	FILE *Infile;	  	 	//<--declare file handler				 //
   	int status, min, max, mid;	//<--declare interger values for binary search		 //
   	float test;			//<--declare float test for binary search division	 //
   	long location;			//<--declare type "long" for the location of data	 //
	char searchName[20];		//<--declare char for user input of search		 //
	bool loopStop = false;
												 //
												 //
												 //
												 //
   	/*Open sorted datafile, check for errors*/						 //
	Infile = fopen ("faculty_sorted.dat","r");						 //
   	if (Infile == NULL)									 //
     	{ 											 //
		fprintf(stderr, "\nError opening input file!\n\n"); 				 //
		exit (1); 									 //
	}											 //
												 //
	/*set min equal to one, needed for search to work*/					 //
	min=1;											 //
	fread (&max, sizeof(int), 1, Infile);	//<--read file					 //
	fprintf(stderr, "\nEnter last name to search (or type 'exit'): "); //<--user input	 //
   	scanf( "%s", searchName ); 					   //of name to search	 //
												 //
	while (strcmp(searchName,"exit")!=0)							 //
	{			           							 //
   												 //
		searchName[0]=toupper(searchName[0]);						 //
												 //
   		mid = (min + (max - min)) / 2;	//<--get midpoint				 //
   		mid--;				//fix midpoint back one, to read the record	 //
												 //
		/*set location by midpoint*/							 //
   		location = sizeof(int) + sizeof(struct Faculty_Info)*mid;			 //
		/*from location search record */						 //
   		fseek (Infile, location, SEEK_SET);						 //
		/*for the wanted record*/		  					 //
   		fread (&TEMP, sizeof(struct Faculty_Info), 1, Infile);	  			 //
		test=max/2;									 //
												 //
		/*while the record is not found, divide search*/				 //
   		while(strcmp(TEMP.lname,searchName)!=0)						 //
   		{										 //
			test=test/2;	//<--size by two (binary search, keep splitting)	 //
												 //
			if (test<=0)								 //
			{									 //
												 //
				/*if test < 0, you do not have the record, 			 //
				  you have fallen out of the list bounds, 			 //
				  print error							 //
				*/								 //
				fprintf(stderr, "\nRecord not found-1\n\n"); 
				
				goto END;	
				
   							   		
			}						   			 //
												 //
			if(strcmp(searchName,TEMP.lname)==0)					 //
			{									 //
				min = mid + 1;	//<--you have found the record, bring up the min //
			}									 //
			else									 //
			{									 //
				max = mid - 1; //<--you have not found the record, keep searching//
			}									 //
												 //	
			mid = (min + (max - min)) / 2; //<--recalculate midpoint		 //
   			mid--;			       //<--push min back			 //
												 //
    			location = sizeof(int) + sizeof(struct Faculty_Info)*mid;		 //
			/*continue to search the list*/						 //
   			fseek (Infile, location, SEEK_SET);			  		 //
   			fread (&TEMP, sizeof(struct Faculty_Info), 1, Infile);			 //
												 //
   		}//while									 //
												 //
		/*										 //
		  if max > or = min, you have searched all records, 				 //
		  its not in the list, print error						 //
		*/										 //
		if(max>=min)									 //	
		{										 //
			fprintf(stderr, "\nRecord not found-2\n\n"); 				 //
			
			goto END;		
			
   					
		}				
			printf("\nFirst Name: %s\n", TEMP.fname);				
			printf("Last Name: %s\n", TEMP.lname);						
			printf("Extension: %d\n", TEMP.phone);						
			printf("Office Number: %d\n", TEMP.office);					 
			printf("Department: %s\n\n", TEMP.dept);
			
						
		
		
		//loopStop = false;
		END:										 //
		/*set min equal to one, needed for search to work*/				 //
		min=1;										 //
		fread (&max, sizeof(int), 1, Infile);	//<--read file				 //
												 //
		/*user input another name or exit code*/					 //
		fprintf(stderr, "\nEnter last name to search (or type 'exit'): "); 		 //
   		scanf( "%s", searchName ); 
									
												 //
	}//while										 //
												 //
	fclose(Infile);	//<--close the datafile							 //
  												 //
}												 //
/*************************************************************************************************/

The goto after a miscompare seems un-necessary. I might be tempted to move the read file, get search name, inside the while loop. This would eliminate the need for the redundant code at END: If you simplify the code in this way it will be much easier to follow the logic.

found the problem, it was a problem with reseting my seek location, below is the working code

/**************************************************************************************************
Devang N. Joshi											  *
Homework Four											  *
CSCI 325 - Binary Search									  *
March 7th, 2011										  	  *
												  *
The purpose of this program is to implement a binary search on sorted data			  *
**************************************************************************************************/
#include <ctype.h>										 //
#include <stdio.h>			//<--header files					 //
#include <stdlib.h>										 //
/*************************************************************************************************/
struct Faculty_Info 										 //
{												 //
   char fname[15],lname[15];									 //
   int phone, office;			//<--data structure for faculty info			 //
   char dept[5];										 //
};												 //
/*************************************************************************************************/
void main ()											 //
{												 //
												 //
	struct Faculty_Info TEMP;	//<--declare a TEMP data structure			 //
   	FILE *Infile;	  	 	//<--declare file handler				 //
   	int status, min, max, mid;	//<--declare interger values for binary search		 //
   	float t= 0;			//<--declare float tfor binary search division	 	 //
   	long location;			//<--declare type "long" for the location of data	 //
	char searchName[20];		//<--declare char for user input of search		 //
												 //
												 //
   	/*Open sorted datafile, check for errors*/						 //
	Infile = fopen ("faculty_sorted.dat","r");						 //
   	if (Infile == NULL)									 //
     	{ 											 //
		fprintf(stderr, "\nError opening input file!\n\n"); 				 //
		exit (1); 									 //
	}											 //
												 //
	/*set min equal to one, needed for search to work*/					 //
	min=1;											 //
	fread (&max, sizeof(int), 1, Infile);	//<--read file					 //
	fprintf(stderr, "\nEnter last name to search (or type 'exit'): "); //<--user input	 //
   	scanf( "%s", searchName ); 					   //of name to search	 //
												 //
	while (strcmp(searchName,"exit")!=0)							 //
	{			           							 //
   											         //
		searchName[0]=toupper(searchName[0]);						 //
												 //
   		mid = (min + (max - min)) / 2;	//<--get midpoint				 //
   		mid--;				//fix midpoint back one, to read the record	 //
												 //
		/*set location by midpoint*/							 //
   		location = sizeof(int) + sizeof(struct Faculty_Info)*mid;			 //
		/*from location search record */						 //
   		fseek (Infile, location, SEEK_SET);						 //
		/*for the wanted record*/		  					 //
   		fread (&TEMP, sizeof(struct Faculty_Info), 1, Infile);	  			 //
		t=max/2;									 //
												 //
		/*while the record is not found, divide search*/				 //
   		while(strcmp(TEMP.lname,searchName)!=0)						 //
   		{										 //
			t=t/2;	//<--size by two (binary search, keep splitting)	         //
			if (t<=0)								 //
			{									 //
												 //
				/*if t< 0, you do not have the record, 			         //
				  you have fallen out of the list bounds, 			 //
				  print error							 //
				*/								 //
				fprintf(stderr, "\nRecord not found\n\n"); 			 //
				goto END;							 //				
   							   					 //
			}						   			 //
												 //
			if(strcmp(searchName,TEMP.lname)==0)					 //
			{									 //
				min = mid + 1;	//<--you have found the record, bring up the min //
			}									 //
			else									 //
			{									 //
				max = mid - 1; //<--you have not found the record, keep searching//
			}									 //
												 //	
			mid = (min + (max - min)) / 2; //<--recalculate midpoint		 //
   			mid--;			       //<--push min back			 //
												 //
    			location = sizeof(int) + sizeof(struct Faculty_Info)*mid;		 //
			/*continue to search the list*/						 //
   			fseek (Infile, location, SEEK_SET);			  		 //
   			fread (&TEMP, sizeof(struct Faculty_Info), 1, Infile);			 //
												 //
   		}//while									 //
												 //
		/*										 //
		  if max > or = min, you have searched all records, 				 //
		  its not in the list, print error						 //
		*/										 //
		if(max>=min)									 //	
		{										 //
			fprintf(stderr, "\nRecord not found\n\n"); 				 //			
			goto END;								 //
												 //
   												 //
		}										 //
			printf("\nFirst Name: %s\n", TEMP.fname);    //|P			 //
			printf("Last Name: %s\n", TEMP.lname);	     //|R			 //	
			printf("Extension: %d\n", TEMP.phone);	     //|I			 //	
			printf("Office Number: %d\n", TEMP.office);  //|N			 //	 
			printf("Department: %s\n\n", TEMP.dept);     //|T			 //
												 //		
		END:										 //
		fseek (Infile, 0, SEEK_SET);	//<--reset the seek value			 //
		/*set min equal to one, needed for search to work*/				 //
											 	 //
		fread (&max, sizeof(int), 1, Infile);	//<--read file				 //
												 //
		/*user input another name or exit code*/					 //
		fprintf(stderr, "\nEnter last name to search (or type 'exit'): "); 		 //
   		scanf("%s", searchName); 							 //
												 //
												 //
	}//while										 //
												 //
	fclose(Infile);	//<--close the datafile							 //
  												 //
}												 //
/*************************************************************************************************/
This question has already been answered. Start a new discussion instead.