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....
Code tags added by Hammer