I've got a little inventory management system going, and it searches for products by name. It also loads old data from a file on the hard disk. For whatever reason, when it loads the name, it doesn't accept the name that it displays in the save/buy functions. If that doesn't make sense, just try loading a name, and using the buy function. It can't find it...
I'm probably just missing it, but could you look at this? Code is posted below.
//main.cpp
#include <iostream>
#include "Inventory.h"
using namespace std;
void sell();
void buy();
void newItem();
int main()
{
int action = -1;
while(action !=0)
{
cout<<"\t\tMenu\n1: Sell\t2: Buy\n3: New Item\t0: Exit\n>";
cin>>action;
switch(action)
{
case -1:
return 0;
break;
case 1:
sell();
break;
case 2:
buy();
break;
case 3:
newItem();
break;
default:
break;
}
}
string a;
cout<<"Waiting";
cin>>a;
return 0;
}
void sell()
{
string name;
unsigned int dexFound = -1;
bool found = false;
cout<<"Enter name: ";
cin>>name;
for(unsigned int i = 0; i<inven.items.size(); i++)
{
if(inven.items[i].name.compare(name) ==0)
{
found = true;
dexFound = i;
break;
}
}
if(found)
{
cout<<"Sell how many? \n>";
int a;
cin>>a;
inven.sell(dexFound,a);
}
else
{
cout<<"Couldn't find item: '"<<name<<"'"<<endl;
}
}
void buy()
{
string name;
unsigned int dexFound = -1;
bool found = false;
cout<<"Enter name: ";
cin>>name;
for(unsigned int i = 0; i<inven.items.size(); i++)
{
if(inven.items[i].name.compare(name) ==0)
{
found = true;
dexFound = i;
break;
}
}
if(found)
{
cout<<"Buy how many? \n>";
int a;
cin>>a;
inven.buy(dexFound,a);
}
else
{
cout<<"Couldn't find item: '"<<name<<"'"<<endl;
}
}
void newItem()
{
Item i = Item();
cout<<"Enter new name: ";
cin>>i.name;
cout<<endl<<"Enter buying price: ";
cin>>i.buyPrice;
cout<<endl<<"Enter selling price: ";
cin>>i.salePrice;
cout<<endl<<"Enter stock: ";
cin>>i.stock;
cout<<endl;
i.finalPrice = i.salePrice * (1 + (salesTax / 100));
inven.addItemType(i);
}
//Inventory.h
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <vector>
#include <string>
using namespace std;
#pragma once
float salesTax = 6;// %
class Item
{
public:
string name;
double salePrice;//what is this sold for
double buyPrice;//what is this bought for
double finalPrice;
int stock;
Item(string newName, double sale,double price, int amount)
{
name = newName;
salePrice = sale;
buyPrice = price;
finalPrice = salePrice * (1 + (salesTax / 100));
stock = amount;
}
Item() {}
bool operator==(Item i)
{
if(i.name == name && i.salePrice == salePrice && i.buyPrice == buyPrice && i.stock == stock)
return true;
return false;
}
};
class Inventory
{
public:
vector<Item> items;
bool addItemType(Item i);//new product
void sell(unsigned int dex,int quan);//sell quan of dex
void buy(unsigned int dex, int quan);//buy from company quan of dex
Inventory(char* loc)//used for customizing the load file
{
Item make;
ifstream a(loc);
string line;
while(!a.eof())
{
getline(a,line);
int dex = 0;
while(line[dex] != ' ' && line[dex] !='\n')//find end of name
{
dex++;
}
if(dex == 0)
{
cout<<"Could not find name of product"<<endl;
}
else
{
make.name = line.substr(0,dex);
}
int start = dex +1;
dex++;
while(line[dex] != ' ' && line[dex] !='\n')//find end of name
{
dex++;
}
if(dex - start > 0)
{
make.buyPrice = atof(line.substr(start,dex).c_str());
}
start = dex + 1;
dex++;
while(line[dex] != ' ' && line[dex] !='\n')//find end of name
{
dex++;
}
if(dex != start && dex > start)
{
make.salePrice = atof(line.substr(start,dex).c_str());
}
start = dex + 1;
dex++;
while(line[dex] != ' ' && line[dex] !='\n')//find end of name
{
dex++;
}
if(dex != start && dex > start)
{
make.stock = atof(line.substr(start,dex).c_str());
}
make.finalPrice = make.salePrice * (1 + (salesTax / 100));
cout<<"'"<<make.name<<"'"<<endl;
items.push_back(make);
}
}
Inventory(bool load)//the standard product load
{
if(load)
{
Inventory("products");
}
else
{
//Starting from scratch
}
}
Inventory() {}
};
bool Inventory::addItemType(Item i)
{
items.push_back(i);
return true;
}
void Inventory::sell(unsigned int dex,int quan)
{
if(items[dex].stock -quan>-1)
{
items[dex].stock -=quan;
return;
}
cout<<"Cannot sell "<<quan<<" "<<items[dex].name<<"s at once.";
}
void Inventory::buy(unsigned int dex,int quan)
{
items[dex].stock +=quan;
}
Inventory inven(true);