-1

Write a program to keep track of a hardware store inventory. The store sells various items. For each item in the store, the following information is kept: item ID, item name, number of pieces ordered, number of pieces currently in the store, number of pieces sold, manufacturer's price for the item, and the store's selling price. Your program must be menu driven, giving the user various choices, such as checking whether an item is in the store, selling an item, and printing the report. After inputting the data, sort it according to the items' names. Also, after an item is sold, update the appropriate counts. Initially, the number of pieces (of an item) in the store is the same as the number of pieces ordered, and the number of pieces of an item sold is zero.

Input to the program is a file consisting of data in the following form:

itemID
itemName
pOrdered manufPrice sellingPrice
1111
Dishwasher
20 250.50 550.50
2222
Microwave
75 150.00 400.00
3333
Cooking Range
50 450.00 850.00
4444
Circular Saw
150 45.00 125.00

Define a vector of class Item to store the information of each item. The program must contain at least the following functions: one to input data into the vectors, one to display the menu, one to sell an item, and one to print the report for the manager.

I am now having issues with my print function, my binary search function, and my sort function (both most likely due to how I'm incorrectly mixing int and string function in my work, but don't know how to fix that). Any help would be greatly appreciated.

#include <iostream>
#include <fstream>
#include <iomanip>
#include <vector>
#include <string>
#include <limits>
#include <algorithm>

using namespace std;

class Item
{
private:
    int itemID = 0;
    string itemName = "";
    int pOrdered = 0;
    double manufPrice = 0;
    double sellingPrice = 0;

public:
    Item() {};//default constructor;
    Item(int ID, string name, int quantityOrdered, double mPrice, double          sPrice) //constructor with parameters for itemID, item name, products ordered, manufacturing price, and selling price
    {
        itemID = ID;
        itemName = name;
        pOrdered = quantityOrdered;
        manufPrice = mPrice;
        sellingPrice = sPrice;
    };  

    friend istream& operator>>(istream& inp, Item& item)
    {
        inp >> item.itemID;
        inp.ignore(numeric_limits<streamsize>::max(), '\n');
        getline(inp, item.itemName);
        inp >> item.pOrdered >> item.manufPrice >> item.sellingPrice;
        inp.ignore(numeric_limits<streamsize>::max(), '\n');
        return inp;
    };

    friend ostream& operator<<(ostream& os, const Item& item)
    {
        os << item.itemID;
        os << item.itemName; 
        os << item.pOrdered; 
        os << item.manufPrice; 
        os << item.sellingPrice; 
        return os; 
    };

};
void menu()
//menu() displays menu 
{
    //Menu choices for hardware store report
    cout << "Welcome to the Friendly Hardware Store!" << endl;
    cout << "Choose among the following options: " << endl;
    cout << "1. To see if an item is in the store. " << endl;
    cout << "2. To buy an item. " << endl;
    cout << "3. To check the price of an item. " << endl;
    cout << "4. To print the inventory. " << endl;
    cout << "9. To end the program. " << endl;
};

void getData(ifstream& inp, vector<Item>& items)
//getData() reads input file into vector of class Item
{
    Item item; 
    while (inp >> item)
        items.push_back(item); 
};

//binSearch returns the index of the data entry if searchItem is found; otherwise, it returns - 1 (not found)
int binSearch(vector<Item> items, string searchItem)
{
    sortData(items); //must sort the data before doing a binary search 
    int first = 0; 
    int size = items.size(); 
    int mid; 
    bool found = false; 

    while (first <= size && !found)
    {
        mid = (first + size) / 2; 
        if (items[mid] == searchItem) //error here w/ "=="
            found = true;
        else if (items[mid] > searchItem) //error here w/ ">"
            size = mid - 1;
        else
            first = mid + 1; 
    }
    if (found)
    {
        cout << searchItem << " is in the store." << endl;
        return mid; 
    }
    else
    {
        cout << searchItem << " is NOT in the store." << endl;
        return -1;
    }
    
};

//printReport() displays all of the data. 
//wont print any values, so is my ostream function written     `incorrectly or is is it my (auto p: items) function--written down below-- instead???`
void printReport(const vector<Item>& items)
{
    cout << "                           Friendly Hardware Store" << endl;
    cout << "itemID       itemName         pOrdered    pInStore    pSold        manufPrice     sellingPrice   " << endl;
    for (auto p : items)
    {
        cout << p << " ";
    }       
    cout << endl; 
};

//sortData() sorts data in the vector 
void sortData(vector<Item>& items)
{
    int min; 
    Item temp; 
    int size = items.size(); 
    for (int i = 0; i < size; i++)
    {
        min = i; 
        for (int j = i + 1; j < size; ++j)
//I get a "no operator "<" matches these operands error on the subsequent line..
            if (items[j] < items[min])
                min = j; 
        temp = items[i]; 
        items[i] = items[min]; 
        items[min] = temp; 
    }// end for 
}//end sort 
        
int main()
{
    menu();
    ifstream infile("storeInventory.txt");
    infile.open("storeInventory.txt"); //open the input file storeInventory.txt
    vector<Item> items;
    string search; 
    getData(infile, items); 
    int choice; //user's choice from the menu
    cin >> choice;
    
    switch (choice)
    {
    case 1:     
        cout << "Enter the name of the item: " << endl;
        cin >> search;
        binSearch(items, search);
        menu(); 
    case 2:
        makeSale(items);
    case 3:
        cout << "To check the price of an item." << endl;
    case 4:
        cout << "To print the inventory." << endl;
        printReport(items);
    case 9:
        cout << "The program will now terminate." << endl;
        break;
    default:
        cout << "Invalid input.";
    } //end switch statement

    infile.close(); //close the input file
    return 0; 
};
Kristen N
  • 1
  • 1
  • Why does `Item` need fixing? Are you sure the methods shouldn't be `static` or even better, be outside? – Quimby Nov 12 '21 at 22:20
  • Does this answer your question? [C++ iostream: Using cin >> var and getline(cin, var) input errors](https://stackoverflow.com/questions/18348350/c-iostream-using-cin-var-and-getlinecin-var-input-errors) – Botje Nov 13 '21 at 00:19
  • 1
    Welcome to StackOverflow. This is a Q&A site, not a homework service, or a code writing service. You are expected to do your own work, and ask *specific* questions as needed. Please take the [tour] and read the [help], as well as [How do I ask and answer homework questions?](https://meta.stackoverflow.com/questions/334822/) and [Open letter to students with homework problems](https://softwareengineering.meta.stackexchange.com/questions/6166/). For future reference, be sure to explain what the *actual problem* is, what is not working for you, and what you have you tried to fix it. Be specific. – Remy Lebeau Nov 13 '21 at 00:46

1 Answers1

1

A few issues I see:

  1. getData() is not handling line breaks between itemID and itemName, and after sellingPrice. The latter can be waved away by the fact that the following call to inp >> itemID for the next Item will skip the line break. However, inp >> itemID is leaving a line break in the input buffer that prevents getline(inp, itemName) from reading correctly (see Why does std::getline() skip input after a formatted extraction?).

  2. getData() is not looping at all, as it is expected to fill the items vector with all of the Items in the ifstream, but it is currently only reading in 1 Item.

  3. main() is attempting to open the storeInventory.txt file twice, once in the ifstream's constructor, and again in its open() method. open() will thus fail since the file is already opened, thus putting the stream into an error state that you are not clear()'ing before continuing to use the ifstream for input.

  4. main() is not calling getData() after opening the ifstream.

With that said, try this instead:

#include <iostream>
#include <fstream>
#include <iomanip>
#include <vector>
#include <string>
#include <limits>

using namespace std;

void getData(ifstream& inp, vector<Item>& items);

//sortData() sorts data in the vector 
void sortData(vector<Item>& items);

//binSearch returns the index of the data entry if searchItem is found; otherwise, it returns - 1 (not found)
int binSearch(vector<Item> items, string searchItem);

//printReport() displays all of the data. 
void printReport(const vector<Item>& items);

//makeSale() prompts user for item and number of pieces and displays the amount due (You don't need to remember total) 
void makeSale(vector<Item>& items);

class Item
{
private:
    int itemID = 0;
    string itemName = "";
    int pOrdered = 0;
    double manufPrice = 0;
    double sellingPrice = 0;

public:
    Item() = default; //default constructor; 
    Item(int ID, string name, int quantityOrdered, double mPrice, double sPrice) //constructor with parameters for itemID, item name, products ordered, manufacturing price, and selling price
    {
        itemID = ID;
        itemName = name;
        pOrdered = quantityOrdered;
        manufPrice = mPrice;
        sellingPrice = sPrice;
    }

    friend istream& operator>>(istream& inp, Item& item)
    {
        inp >> item.itemID;
        inp.ignore(numeric_limits<streamsize>::max(), '\n');
        getline(inp, item.itemName); 
        inp >> item.pOrdered >> item.manufPrice >> item.sellingPrice;
        inp.ignore(numeric_limits<streamsize>::max(), '\n');
        return inp;
    }
};

//getData() reads input file into vector of class Item
void getData(ifstream& inp, vector<Item>& items)
{
    Item item;
    while (inp >> item)
        items.push_back(item);
};

int main()
{
    menu(); //call menu function 
    ifstream infile("storeInventory.txt"); //open the input file 
    if (!infile)
    {
        cout << "Cannot open the input file.The program terminates. ";
        return 1; 
    }
    vector<Item> items;
    getData(infile, items);
    infile.close(); //close the input file
    // use items as needed...
    system("pause");
    return 0; 
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770