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

Recommended Answers

All 5 Replies

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

in the header, there should be a } then on the next line put else { return false; }

after line 13 there should be a { and after line 14 should be a } then an else if statement with if(i.name != name && i.salePrice != salePrice and so on and put { return false; }

It compiles, but I've got a runtime error, probably because the constructor of class inventory requires a file called "products". Also reading the lines until !eof will make the last element appear twice in your vector, but that's not the real issue. I don't want to be rude, but the way you organize your code makes it really hard to read, and understand what's going on.

One example, when I looked at it, I thought you forgot to make an instance of your class, and that's what it causing the problem. - I didn't see any instance of "Inventory" nor "Item" in your main, but then it compiled, so I thought it must be somewhere. I kept looking and I found this line at the end of Inventory.h : Inventory inven(true); . This is not good, you are not suppose to do that, if you need a global variable put it in the main.cpp. And what's the point of having a constructor which is only for to call another constructor? Your instance of "Inventory" will exist in the scope of your program regardless if load was true or false, and when your functions try to access the instance of the class, things can get really ugly.

Inventory(bool load)//the standard product load
    {
        if(load)
        {
            Inventory("products");
        }
        else
        {
            //Starting from scratch
        }

    }

Compare is very strict for finding strings, because it doesn't leave any room for errors, the two strings have to be an exact match. - As a user I would prefer something more flexible. One question, are you sure there are no hidden white space in the name field of item? Maybe that is the reason why your functions unable to match them. If I were you I would make a display function for the class inventory, and see what's really inside it. Just to make sure everything is properly initialized.

@Zvjezdan23 Braces are not necessary there, he is fine without them.

I saw a '=' operator, it didn't look right.

Do you know of a better method, or should I loop through all of the characters?

Thanks,
Jack

An explanation of my three constructors:
(char*): this is for loading the products from a custom location.
(bool): true: loads from default products file. false: no product loading.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.