1

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:

  1. Display the inventory sorted by sku. (displayInventory function)
  2. Lookup a product by sku. (lookupSku function)
  3. Lookup a product by name. (lookupname function)
  4. 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;
  }
anothermh
  • 6,482
  • 3
  • 20
  • 39
  • 2
    *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.* -- Your program has a bug. Have you done any debugging yourself, even minimal debugging such as printing out what the value of `middle` is? – PaulMcKenzie Feb 19 '18 at 06:30
  • 1
    Is the file pre-sorted? You yourself don't appear to sort anything, so what makes option 2 well defined? – StoryTeller - Unslander Monica Feb 19 '18 at 06:32
  • You report a _random_ output. Does this mean that you get different results when you rerun your code with the same inputs (file and sku number)? – AchimGuetlein Feb 19 '18 at 06:40
  • At first, make sure that file is sorted. Otherwise, the binary search won't work. – Naseef Chowdhury Feb 19 '18 at 07:09
  • Unrelated, but recommended, reading: https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong – user4581301 Feb 19 '18 at 07:11
  • As already mentioned, make sure the input is sorted and also that the SKU number entered exists. Also, you need to check that found == true after your while loop (if you are not checking that the SKU exists). – falopsy Feb 19 '18 at 08:05

0 Answers0