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;
};