Anyone can help me?
I have inventory system which works with item-bags.
There are 4 different item-bags. 1 bag have 4 other 8, 12, 16.

Now for the NPC shop I need to check if there is a free place in my inventory.

Lets say, I have 2 bags. 1 Bag with 16 slot and the second bag has 4 slots.
Bag 1 in database = slot 0 & place 0
Bag 2 in database = slot 0 & place 1

Items which are in bag 1 = slot 1 & place 0-15 (total 16)
Items which are in bag 2 = slot 2 & place 0-3 (total 4)

Now my big problem is how to check if one of those inventory have free place?

Any ideas? How I should do this or anything?

One way (off the top) could be ...

struct Bag4
{
    bool filled;
    string items[4];

    Bag4() : filled(false) {}    
} ;

// etc ...

Really ... one does need more info and the particulars of your inventory data management problem ?

Another way ...

struct Bag
{
    string name;
    vector< string > slots;

    Bag() {}
    Bag( const string& name, size_t n )
        : name(name)
    {
        slots.reserve(n); // or maybe resize(n) ? //
    }
} ;

// ...

vector < Bag > store;

Hmm sorry but I dont really get this :s

Well ... there are many ways to go about your problem.

If you have no particular design constraints or requirements (beyond those listed below) ...

this next little demo may give you some more ideas:

// inventoryBagSystem.cpp //

/*
    I have inventory system which works with item-bags.
    There are 4 different item-bags, sizes: 4, 8, 12 and 16

    ...

    Now my big problem is how to check if one of those
    inventory (slots) have (a) free place?
*/

#include <iostream>
#include <iomanip> // re. setw
#include <sstream> // re. stingstream obj's
#include <string>
#include <vector>

#include <algorithm> // re. vector find ...

using namespace std;

const string HEADER =
    "This demo will ask for a (next) bag name ...\n"
    "and then asks for that bag size.\n"
    "Then, asks for the name of an item "
    "to put in a slot in that bag,\n"
    "until done for that bag, or that bag is filled.\n"
    "Then, will loop for next bag name and size "
    "... until 'done'.";


class Bag
{
public:
    Bag() {}
    Bag( const string& name="", size_t n=16 ) : name(name), max_len(n)
    {
        slots.reserve(max_len);
    }
    size_t size() const { return slots.size(); }
    size_t max_size() const { return max_len; }
    bool is_filled() const { return (max_len == slots.size()); }

    void set_name( const string& name )
    {
        this->name = name;
    }

    string get_name() const { return name; }

    bool add_item()
    {
        if( !is_filled() )
        {
            cout << "Item to add: " << flush;
            string item;
            getline( cin, item );
            slots.push_back( item );
            return true;
        }
        // else ...
        cout << "\nBag " << name << " is filled.\n";
        return false;
    }

    bool remove_item( const string& item )
    {
        vector< string >::iterator it;
        it = find( slots.begin(), slots.end(), item );
        if( it != slots.end() )
        {
            slots.erase( it );
            cout << "\nItem " << item << " was removed ok.\n";
            return true;
        }
        // else ...
        cout << "\nItem " << item << " not found.\n";
        return false;
    }
private:
    string name;
    size_t max_len;
    vector< string > slots;

    // show bag contents ...
    friend ostream& operator << ( ostream& os, const Bag& bg )
    {
        os << "For 'bag' " << bg.name << ", the contents are:\n";
        if( bg.size() )
        {
            vector< string >::const_iterator it;
            int count = 0;
            for( it = bg.slots.begin(); it != bg.slots.end(); ++ it )
            {
                os << "In slot " << setw(2) << (++count)
                   << ": " << *it << endl;
            }
        }
        else os << "The bag is empty ...\n";
        return os;
    }
} ;


typedef vector< Bag >::iterator Bagit;

Bagit findBag( Bagit beg, Bagit end, const string& name )
{
    for( Bagit it = beg; it != end; ++ it )
    {
        if( it->get_name() == name )
            return it;
    }
    // else ...
    return end;
}

void show( const vector< Bag >& store )
{
    vector< Bag >::const_iterator it;
    for( it = store.begin(); it != store.end(); ++ it )
    {
        cout << *it << endl;
    }
}


int main()
{
    cout << HEADER << "\n\n";

    vector< Bag > store;

    // loop to enter each bag name and fill each bag with items
    while( true )
    {
        cout << "Enter next bag name: " << flush;
        string name;
        getline( cin, name );

        // loop to get valid bag (max) size ...
        size_t size;
        while( true )
        {
            cout << "Enter bag size (4, 8, 12, 16) : " << flush;
            string tmpStr;
            getline( cin, tmpStr );
            istringstream iss( tmpStr ); // construct iss from tmpStr
            iss >> size;
            if( size == 4 || size == 8 || size == 12 || size == 16 )
                break;
            // else ...
            cout << "NOT a valid bag size ... try again\n";
        }

        Bag tmpBag( name, size ); // call ctor...

        // now loop to fill this bag with items ...
        // while room and more //
        string reply;
        while( tmpBag.add_item() )
        {
            if( tmpBag.is_filled() )
            {
                cout << "Bag " << tmpBag.get_name()
                     << " is filled.\n";
                 break;
            }
            cout << "More to add (y/n) ? " << flush;
            getline( cin, reply );
            if( reply != "y" )
            {
                cout << "Done 'adding loop' ... \n";
                break;
            }
        }

        cout << "\nAdding bag " << tmpBag.get_name()
             << " to store ...\n";
        store.push_back( tmpBag );

        cout << "More BAGS (y/n) ? " << flush;
        getline( cin, reply );
        if( reply != "y" )
        {
            cout << "Done 'adding BAGS loop' ... \n";
            break;
        }
    }


    // show store ...
    cout << "\nThe store contents now ... \n\n";
    show( store );




    // remove items ...

    // find bag and item to remove ...
    cout << "\nDemo removing items ... \n\n";
    while( true )
    {
        cout << "\nEnter bag name: ";
        string bag_name;
        getline( cin, bag_name );

        Bagit it = findBag( store.begin(), store.end(), bag_name );
        if( it != store.end() )
        {
            // remove item loop ...
            //while( true )
            //{
                cout << "\nEnter item name: ";
                string item_name;
                getline( cin, item_name );

                it->remove_item( item_name );
            //}
        }
        else
        {
            cout << "\nBag " << bag_name << " was not found ...\n";
            cout << "Exit or Continue (e/c) ? " << flush;
            string reply;
            getline( cin, reply );
            if( reply == "e" )
            {
                cout << "Exiting 'remove items loop' ... \n";
                break;
            }
            //else continue; // enter next bag name ...
        }

        // show store ...
        cout << "\nThe store contents now ... \n\n";
        show( store );

        cout << "Exit or Continue (e/c) ? " << flush;
        string reply;
        getline( cin, reply );
        if( reply == "e" )
        {
            cout << "Exiting 'remove items loop' ... \n";
            break;
        }
    }
}
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.