# Thread: Binary Search on an Array of Struct

1. ## Binary Search on an Array of Struct

Hello....

I am having a hard time trying to make a record try and search an item code that a user would enter and then match it with the record from the struct.
Code:
```// c++ code:
sturct salesrecord
{
char item_code[8];
int quantity;
float price;
}
salesrecord Sales;

char code;
int first;
int last;
int mid;
.
.   // blah blah blah

//Here is what I wrote for the binary search that does not work...

cin >> code;

first = 0;
last = max;

// binary search
while ( (first <= last) && (Sales[mid].item_code != code) )
{    mid = (first + last / 2) ;

if (Sales[mid].item_code < code)
{ //look in upper half
first = mid + 1;
}
else
{ //look in lower half
last = mid - 1;
}
mid = (first + last) / 2;
}
if (Sales[mid].item_code == code)   //match
{
......```
I tried everything for it to search but it still does not want to work....can i get any help out there??? Thanks in advanced....

2. Recommendations

1. Make it an array of structs

2. when using a binary search the data has to be sorted allready.

3. Don't "reinvent the wheel" look at books and other sources for binary search code and modify it to fit your needs.

3. >>Recommendations
... and another, please use code tags (see my signature for help on this).

4. ## Tying again...

/* This is what I wrote.. */
Code:
```#include <string.h>     //strcmp
#include <iomanip.h>
#include <fstream.h>	//file stream and cin ,cout
#include <iostream.h>	//setiosflags, setprecision

const int max = 9;                    // 10 records in file

struct SalesRecord		// array of struct (which is sorted)
{
char item_code[8];                   //given
int quantity_on_hand;
float unit_selling_price;
};

void main (void)
{
SalesRecord Sales[max];

ifstream ifsServ;   //Input file stream
ofstream ofsServ;   //Ouput file stream
char inputfile[30];
char outputfile[30];

cout << "Enter input path: " << endl;
cin >> inputfile;
cout << "Enter output path: "<<endl;
cin >> outputfile;

ifsServ.open(inputfile);   //Open the input file stream
ofsServ.open(outputfile);  //Open the output file stream
char code[8];
int quantity;
float value;

int lowx = 0;        //index ranges
int hix = max -1;
int midx = 0;
int i = 0;

if (!ifsServ)
{
cout << "Error opening input file" <<endl;
}
else
{
if (!ofsServ)
{
cout << "Error opening output file" << endl;
}
else
{

ifsServ >> Sales[i].item_code[8]
>> Sales[i].quantity_on_hand
>> Sales[i].unit_selling_price;
}
}
cout << "Enter Item Code: " ;
cin >> code;

while (strcmp (code,"quit") !=0)
{
cout << "Enter Quantity Order: " ;
cin >> quantity;

// binary search (in the book)
midx = (hix + lowx) /2;
while ( (hix > lowx) && (Sales[midx].item_code != code))
{
if (Sales[midx].item_code < code)
{
lowx = midx+1;
}
else
{
hix = midx - 1;
}

midx = (hix + lowx) / 2;
}
if (Sales[midx].item_code == code)                 //if found
{
cout << "Found a MATCH!" << endl;
}
else
{
}

cout << "Enter Item Code: " ;
cin >> code;

i++;
ifsServ >> Sales[i].item_code[8]
>> Sales[i].quantity_on_hand
>> Sales[i].unit_selling_price;
}
}```
/*
It compiles but cannot find a match with the code entered by the user? For example enter code = ABC1110 and in the record there is a ABC1110 in item_code. Any specific help would be aprreciated! */

&#91;code]&#91;/code]tagged by Salem