0

I have written a program which should do the following:

read product item data from an inventory file and add them in a vector object. The program will allow user to view, search and order the product items. The inventory file should be updated after the order of any item.

  • For operation #2 (searching) and #3 (ordering), appropriate messages should be displayed for the input of invalid item name, then program should proceed to process next operation

Expected output:

>: 2
Enter the item name to search: table
table not found.

>: 2
Enter the item name to search: Microwave
Microwave is in stock.
  • For operation #3 (ordering), program should validate the input number of order and make sure there are enough items in the inventory. Otherwise, display error message and skip the ordering –

Expected output:

>: 3
Enter the name of the item: Microwave
Enter the nuber of items: 100
Insufficient inventory.

>: 3
Enter the name of the item: Microwave
Enter the nuber of items: 2
Total cost is $300

>: 3
Enter the name of the item: table
table not found.

but when I try to search for the name of the item by name using binary search, it doesn't give me the result I want, example:

1.

>: 2
Enter the item name to search: Cooking Range
Cooking Range not found.
>: 2
Enter the item name to search: Circular Saw
Circular Saw not found.

This is the file that contains the data:

itemData.txt

1111
Dish Washer
20 550.5
2222
Microwave
75 150
3333
Cooking Range 
50 850
4444
Circular Saw
150 125

And this is the definition of the function I tried to implement to search the items by name:

int
searchItemByName(vector<Item> items, string searchName)
{
    int low, high, mid;

    low = 0;
    high = items.size() - 1;
    while (low <= high) {
        mid = (low + high) / 2;

        if (items[mid].getName() == searchName)
            return (mid);
        else if (items[mid].getName() > searchName)
            high = mid - 1;
        else
            low = mid + 1;
    }
    return (-1);
}

Here is the entire program (I've also added a clearer and broader "description" of what the program should do with some tests.):

https://github.com/jrchavez07/project_07

1
  • 5
    Note: binary search assumes that the array being searched is ordered by the search key. Commented Apr 6, 2022 at 11:34

1 Answer 1

1

I checked your GitHub project.

I think you have to sort data before binary search.

Current Data in the sample text file list like this:

Dish Washer
Microwave
Cooking Range 
Circular Saw

And these are not sorted, so you can not use Binary Search.

Sign up to request clarification or add additional context in comments.

1 Comment

Yeah, that's right, I already solved it. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.