I am having issues with binary search function within my homework (I am completely done with it except for this one issue).
The program allows a user to manage the inventory of a small store that sells various products of any type. It will first read the inventory from a text file named “inventory.dat”, reading the product name, sku, quantity, and price. It will contain up to 100 products.
Then, it should offer the user a menu with the following options:
- Display the inventory sorted by sku. (displayInventory function)
- Lookup a product by sku. (lookupSku function)
- Lookup a product by name. (lookupname function)
- Quit
The program should perform the selected operation and then re-display the menu.
When I select option 2 and enter a sku number, my program outputs a random item's information instead of the one that was typed in by the user.
Here is my code starting in the main function along with the lookupBySku function:
int main()
{ 
// Initialize variables & array
int userInput;
//max array size
const int MAX_SIZE = 100; //const to make it constantly be 100
//declare and open input file
ifstream fin;
fin.open("inventory.dat");
//declare strings to hold data
string productName, sku, quantity, price;
if (!fin)  // checking if file was inputted correctly or not.
{
    cout << "File not found! Try again. " << endl;
    return 1;
}
    Inventory items[MAX_SIZE];
    Inventory temp[MAX_SIZE];
cout << left << setw(20) << "Product Name " <<
left << setw(10) << "sku #" <<
left << setw(3) << "Quantity " <<
left << setw(0) << " Price\n";
//declare number of objects in inventory
int numOfObjects = 0;
while(!fin.eof())
{
    getline(fin, productName);
    getline(fin, sku);
    getline(fin, quantity);
    getline(fin, price);
    if(productName == "")
    {
    break;
    }
    items[numOfObjects].name = productName;
    cout << left << setw(20) << items[numOfObjects].name;
    items[numOfObjects].sku = sku;
    cout << left << setw(10) << items[numOfObjects].sku;
    items[numOfObjects].quantity = quantity;
    cout << left << setw(4) << items[numOfObjects].quantity;
    items[numOfObjects].price = price;
    cout << left << setw(0) <<"\t\t" << items[numOfObjects].price << endl;
    ++numOfObjects;
}
//check to see if there are more than 100 items
if(numOfObjects > 100)
{
    cout << "File is too large, terminating program...\n";
    return -1;
}
// Constants for menu choices
const int DISPLAY_BY_SKU = 1,
LOOKUP_BY_SKU = 2,
LOOKUP_BY_NAME = 3,
QUIT_CHOICE = 4;
do
{
// Set up numeric output formatting.
cout << fixed << showpoint << setprecision(2);
cout << "Manage Inventory Menu\n" <<endl;
cout << "1. Display inventory sorted by sku. " <<
"\n2. Lookup a product by sku. " <<
"\n3. Lookup a product by name. " <<
"\n4. Quit the program\n" << endl;
cout << "Enter your choice : ";
cin >> userInput;
cout << endl;
while (userInput < DISPLAY_BY_SKU || userInput > QUIT_CHOICE)
{
    cout << "Please enter a valid menu choice: ";
    cin >> userInput;
}
   switch(userInput)
    {
        case DISPLAY_BY_SKU:
        {
          displayInventory(items, numOfObjects);
          break;
        }
        case LOOKUP_BY_SKU:
        {
        lookupSku(items, numOfObjects);
        break;
        }
        case LOOKUP_BY_NAME:
       {
        lookupName(items, numOfObjects);
        break;
       }
    case 4:
        cout << "Exiting the program." <<endl;
        break;
    }
}
while (userInput != QUIT_CHOICE);
fin.close();
return 0;   }
 void lookupSku (Inventory items[], int numOfObjects)  {
     string number;
     int first = 0;
     int last = numOfObjects - 1;
 int middle;
 int position = -1;
 bool found = false;
 cout << "Enter the sku that you'd like to search for : ";
 cin >> number;
 cout << endl;
     while(!found && first <= last)
     {
         middle = (first + last)/2;
         if(items[middle].sku == number)
         {
             found = true;
             position = middle;
         }
             else if(items[middle].sku > number)
             {
                 last = middle - 1;
             }
                 else if(items[middle].sku < number)
                 {
                     first = middle + 1;
                 }
        }
 cout << "Your item is shown below : \n" << endl
 << "Product Name : "<< items[middle].name << endl
 << " Sku : " << items[middle].sku << endl
 <<" Quantity : " << items[middle].quantity << endl
 << " Price : " << items[middle].price << endl
 << endl;
  }
 
    