This program keeps track of a hardware store inventory. It's very barebones, all it needs to do is put the information into a vector of pointers to a struct, allow the user to sell items, and print the report. I have printing down no problem, but selling is where I run into problems. I just can't think of a good way to do it. This is what I have right now.

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <conio.h>
#include <vector>
#include <algorithm>

using namespace std;




struct SInventory
{
	string ItemID;
	string itemName;
	int pOrdered, pInStore, pSold;
	double mPrice, sellPrice;
	static int count;
	static double totalPrice;
};

struct FindByName 
{
    string ID;
    FindByName(const string& ID) : ID(ID) {}
    bool operator()(SInventory* s) { return s->ItemID == ID; }
};

int SInventory::count;
double SInventory::totalPrice;

class print
{
public:
	void operator ()(SInventory* ptr)
	{
		ptr->pInStore = ptr->pOrdered - ptr->pSold;
		cout << left << setw(8) << ptr->ItemID << setw(20) << ptr->itemName << setw(8) << ptr->pOrdered << setw(8) << ptr->pInStore << setw(8) << ptr->pSold << setw(10) << setprecision(2) << fixed << ptr->mPrice << setw(10) << ptr->sellPrice << endl;
		ptr->count += ptr->pInStore;
		ptr->totalPrice += ptr->pInStore * ptr->sellPrice;
	}
};

char menuChoice();
void addItems(vector<SInventory*>& vec);
void sellItem(vector<SInventory*>& vec);

int main()
{
	vector<SInventory*> vec;
	char menu = ' ';
	addItems(vec);
	while (menu != 'c')
	{
		menu = menuChoice();
		switch(menu)
		{
		case 'a':
			sellItem(vec);
			break;
		case 'b':
			cout << endl << left << setw(8) << "Item ID" << setw(20) << "Item Name" << setw(8) << "Order" << setw(8) << "Store" << setw(8) << "Sold" << setw(10) << "M. Price" << setw(10) << "S. Price" << endl << endl;
			for_each(vec.begin(), vec.end(), print());
			cout << endl << "Total Inventory: " << SInventory::totalPrice << endl << "Total number of items in store: " << SInventory::count << endl << endl;
			break;
		case 'c':
			break;
		default:
			cout << "Invalid choice";
			break;
		}
	}
			_getch();
	return 0;
}

char menuChoice()
{
	char menu;
	cout << "a. Sell Item" << endl << "b. Print Report" << endl << "c. Exit" << endl;
	cout << "\nEnter a choice ";
	cin >> menu;
	return menu;
}

void addItems(vector<SInventory*>& vec)
{
	char str[99];
	SInventory temp;
   ifstream i;

   i.open("items.txt");
	while(!i.eof())
	{
		i.getline(str, 99);
		temp.ItemID = str;
		i.getline(str, 99);
		temp.itemName = str;
		i >> temp.pOrdered;
		i >> temp.mPrice;
		i >> temp.sellPrice;
		temp.pSold = 0;
		temp.pInStore = 0;
		vec.push_back(new SInventory (temp));
		i.getline(str, 99);
	}
}

void sellItem(vector<SInventory*>& vec)
{
	string no;
	int sell;
	cout << "Enter the item number of the item you want to sell";
	cin >> no;
	vector<SInventory*>::iterator it = find_if(vec.begin(), vec.end(), FindByName(no));
	cout << "You are selling: " << endl << *it;
	cout << endl << "How many do you want to sell? ";
	cin >> sell;
}

I tried getting the information out of the iterator, but for some reason, when I print it as a pointer, all I get is it's address, and if I take the * out, I get this error:

Error	1	error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::_Vector_iterator<_Ty,_Alloc>' (or there is no acceptable conversion)	c:\users\andrew\documents\visual studio 2008\projects\project 5\project 5\project 5.cpp	118

Same goes for what I try after sell. I tried using it->ItemID, *it->ItemID, dot notation, and it all gives me 2 errors:

Error	1	error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::_Vector_iterator<_Ty,_Alloc>' (or there is no acceptable conversion)	c:\users\andrew\documents\visual studio 2008\projects\project 5\project 5\project 5.cpp	118
Error	3	error C2039: 'ItemID' : is not a member of 'std::_Vector_iterator<_Ty,_Alloc>'	c:\users\andrew\documents\visual studio 2008\projects\project 5\project 5\project 5.cpp	121

So right now I'm at a loss of what to do. If anyone can point me in the right direction, I'd be very grateful.

This article has been dead for over six months. Start a new discussion instead.