• C

Binary Search using characters

I'm trying to do a binary search where the user types in the state abbreviation and it searches through a states table and if it is a valid state then that state is stored into the myRecords structure for states. I keep getting an unhandled exception error when I try to run it.
#include <ctype.h>
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <stdio.h>

struct customers
{
	int cust_id;
	char cust_name[28];
	char states[3];
	char discount;
	double balance_due;
	int order_out;
};

struct myRecords
{
	customers rec[100];
};

struct state
{
	char name[15];
	char abbr[3];
};

struct stateType
{
	state lists[51];
};

using namespace std;

int is_int(char [], char []);
int get_orders(char[], char[]);
int checkState(struct myRecords*, struct stateType*, char[]);
char get_code(char[], char[]);
char get_char(char[]);
float get_bal(char[], char[]);
void is_name(char [], char []);
void get_str(char[], char[]);
void addRecord(struct myRecords*);
void listFile(struct myRecords*);

int main()
{
	myRecords table, *point_table;
	point_table = &table;

	char cust;
	memset (point_table, 0, sizeof (table));

	addRecord(point_table);
	cust = get_char ("Would you like to enter another return? ");
	cust = toupper(cust);

	while (cust == 'Y')
	{
		addRecord(point_table);
		cust = get_char ("Would you like to enter another return? ");
		cust = toupper(cust);
	}//end while
	
	listFile(point_table);
	system("pause");
	return 0;

}//end of main

/******************************
*Binary search for  the state *
******************************/
int checkState (struct myRecords* table, struct stateType* STname, char stateABB[])
{
	static char f_time;
	f_time = 'Y';
	if (f_time == 'Y')
	{
		char buffer[50];
		FILE *fp;
		fp = fopen("STATES.DAT", "r");
		if (fp == NULL)
		{
			printf ("Error opening file! ");
		}
		 
		int counter = 0;
	
		while(!(feof(fp)))
		{
			
			fgets(STname->lists[counter].name, 20, fp);
			STname->lists[counter].name[strlen(STname->lists[counter].name)-1] = '\0';
			fgets(STname->lists[counter].abbr, 20, fp);
			STname->lists[counter].abbr[strlen(STname->lists[counter].abbr)-1] = '\0'; 
						
			++counter;
		}
		fclose(fp);
		f_time == 'N';
	}
	
	
	int first=0,last = 50, middle, position=-1;
	bool found=false;
	
	while (!found && first <= last)
	{
		int Compare;
	    middle = (first + last) / 2;
		Compare = strcmp (STname->lists[middle].abbr, stateABB);
		if (Compare == 0)
		{
			found = true;
			position = middle;
		}
		else if (Compare > 0)
		{
			last = middle - 1;
		}
		else
		{      
			first = middle + 1;
		}
	}
	if (position == -1)
	{
		printf ("Not a valid state abbreviation");
		get_str("Please enter a state: ", stateABB);
	}
	else 
	{
		strcpy (stateABB,  STname->lists[position].abbr);
	}
	return position;
	
}
/**************************
*Function to add a record *
**************************/
void addRecord (struct myRecords *table)
{
	stateType STname, *point_STname;
	point_STname = &STname;

	char buf[50];
	char name[50];
	char stateABB[3];
	char code[50];
	char order[50];
	char bal[50];
	
	char string[50];
	int x;
	int y = 0;
	for (x=0; x < 100; x++)
	{	
		if (table->rec[x].cust_id == 0)
		{
			y = x;
			break;	
		}
	}
	table->rec[y].cust_id = is_int("Please enter a customer id: ", buf);
	is_name("Please enter customer name: ", table->rec[y].cust_name);
	get_str("Please enter a state: ", stateABB);
	checkState(table, point_STname, stateABB); 
	table->rec[y].discount = get_code("Please enter discount code: ", code);
	table->rec[y].order_out = get_orders("Please enter outstanding orders: ", order);
	table->rec[y].balance_due = get_bal("Please enter balance due: ", bal);
	y++;
}
/***********************
*Function to list file *
***********************/
void listFile(struct myRecords *table)
{
	int x;
	printf ("Cust ID\t Cust Name\t       State   Disc.     Bal Due      Order Out\n");
	for (x=0; x < 100; x++)
	{	
		if(table->rec[x].cust_id == 0)
		{
			x = 100;		
		}
		else
		{
			printf ("\n%i\t%-20s\t%-7s\t%c\t%0.2f\t\t%i", table->rec[x].cust_id, table->rec[x].cust_name, table->rec[x].states, table->rec[x].discount, table->rec[x].balance_due, table->rec[x].order_out);
		}
	}
}

Open in new window

reesee324Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Infinity08Commented:
The exact and complete error message would be useful. So, can you post it here ?


Btw, this is C++ code, not C code, so this question really belongs in the C++ zone.
0
reesee324Author Commented:
Sorry I must have hit the wrong one. Here ya go:
Unhandled exception at 0x000614ea in Reynolds Assignment 4.exe: 0xC0000005: Access violation writing location 0x0000e44b.
0
Infinity08Commented:
Did you try running your code in a debugger to get an accurate location of where this error occurs ?
0
reesee324Author Commented:
Yeah it starts where the cust = get_cust("...") but when I comment out the checkState in the addRecord function the program works so it's something in my binary search that is throwing it off and I don't know if it is how I'm returning it or what.
0
Infinity08Commented:
>> Yeah it starts where the cust = get_cust("...")

I don't find any such line in the code you posted.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.