Basically, its all to do with the inventory system, all the other reams of code work fine, thank God!

with the inventory, the idea is that each character can hold 5 items, so an array, but the array needs to hold the itemID and the itemName therefore an array wont be any good coz it can only hold one or the other. so, we been told to create an array where each element is a class (in my case class called itemClass), i have create an array of int in the characters baseclass and then trying to tell it in the add item function that the array is made up of itemClass.

if u run the code, u get an error about a missing primary expression. i dont know if i should be running the additem function from the itemClass or the characters Class, i have tried both :s - lecturers are being useless coz one says do it with a struct the other says do it this way

driving me crazy lol

Any pointers appreciated

#include <iostream.h>
#include <string.h>
#define INVENTORYLIMIT 5
using namespace std;

class characters //name of base class
{
      private:
        int currentLocationID;
        int healthPoints;
        int attackPoints;
        string charName;
        string description;
      protected:
        int inventory[INVENTORYLIMIT]; // declares an array
        int noItemsInInventory;
      public:
        characters(); //A constructor function - must be same name as class itself
        ~characters(); // a destructor function
        int getLocation();
        bool setLocation (int charlocation);
        string getName(); // An accessor function for name
        bool setName(string name); // An mutator function for name
        string getDescription(); // Accessor function for description
        bool setDescription (string chardescription); // A mutator function for description
        bool setHealthPoints(int health); // a mutator function for health points
        int getHealthPoints(); // an accessor function for health points
        bool setAttackPoints(int attack); // a mutator function for attack points
        int getAttackPoints(); // an accessor function for attackpoints
        ///////////////////////////////
        bool addAttackPoints(int newAttackPoints); // a mutator function to subtract attack points
        bool subtractHealthPoints(int subtractHealthPoints); // a mutator function for subtracting Health Points
        bool subtractHitPoints(int subtractHitPoints); // a mutator function for subtracting hit points (attack points)
        //////////////////////////////

};
 ////////////////////////////////////////////////////////////////////////////////
class playingCharacterClass: public characters
{
    private:
        int expPoints;
    public:
        bool addExperiencePoints(int points);// a mutator function to add experience points
        bool setPoints(int expPoints); // A mutator function for expPoints
        int getPoints (); // An Accessor for expPoints
};
 ////////////////////////////////////////////////////////////////////////////////
class nonPlayingCharacterClass: public characters
{
    private:
       bool isAlive;
       string speakTip;
};
 ////////////////////////////////////////////////////////////////////////////////
class itemClass
{
    private:
       int itemID;
       string itemName;
    public:
       bool addItem(int itemID, string itemName);
        //bool dropItem (int itemID);

};


 /// constructor function // is automatically called when a new object is called
characters::characters()
{
    for (int i=0;i<INVENTORYLIMIT;i++)
   {
       inventory[i]=0;
   }
   noItemsInInventory=0;

   cout << "A character has been born!" << endl;
}
/// Destructor Function /// is automatically called when the scope of its existance ends
characters::~characters()
{
   cout << "Your character died!" << endl;
}
/// Accessor Function for expPoints ///
int playingCharacterClass::getPoints()
{
    return expPoints;
}
// Mutator function for expPoints ///
bool playingCharacterClass::setPoints(int points)
{
     expPoints=points;
}
/// Accessor Function for location ///
int characters::getLocation()
{
    return currentLocationID;
}
// Mutator function for location ///
bool characters::setLocation(int charlocation) // charlocation contains the variable send up from the setLocation function in main
{
     currentLocationID=charlocation;
}
/// Accessor function for name /// need accessor for when we recall the data later in the "test harness"
string characters::getName()
{
       return charName;
}
/// Mutator fuction for name ///
bool characters::setName(string named)
{
     charName=named;
}
 /// Accessor function for description ///
 string characters::getDescription()
{
        return description;
}
/// Mutator function for description ///
bool characters::setDescription (string chardescription)
{
     description=chardescription;
}
/// Accessor for health points
int characters::getHealthPoints()
{
    return healthPoints;
}
/// Mutator for health points
bool characters::setHealthPoints (int health)
{
    healthPoints=health;
}
/// Accessor for attack points
int characters::getAttackPoints()
{
    return attackPoints;
}
/// Mutator for attack points
bool characters::setAttackPoints (int attack)
{
    attackPoints=attack;
}
////////////////////////////////////////////////////////////////////////////////
/// mutator function to subtract attack points
bool characters::subtractHitPoints(int subtractHitPoints)
{
     attackPoints=attackPoints-subtractHitPoints;
}
/// mutator function to subtract health points
bool characters::subtractHealthPoints(int subtractHealthPoints)
{
    healthPoints=healthPoints-subtractHealthPoints;
}
 /// Mutator function for addExperiencePoints
 bool playingCharacterClass::addExperiencePoints(int points)
{
     expPoints=expPoints+points;
}
/// Mutator function for addAttackPoints
bool characters::addAttackPoints(int newAttackPoints)
{
    attackPoints=attackPoints+newAttackPoints;
}
////////////////////////////////////////////////////////////////////////////////
// mutator - Add an item to inventory
bool itemClass::addItem(int itemID, string itemName) // this will put the itemID in first availble slot.
{
    itemClass inventory[INVENTORYLIMIT];

    for(int i=0;i<INVENTORYLIMIT;i++)
    {
        if (inventory[i].itemID=0)
        {
            inventory[i].itemID=itemID;
            inventory[i].itemName=itemName;
        }
    }

}

int main(void)
{
   playingCharacterClass character1; // creates object called character1

   int itemID;
   string itemName;
   cout << "Enter the itemID if item to be added to the inventory: ";
   cin >> itemID;
   cin.ignore();
   cout << "Enter the description of the item to be added to the inventory: ";
   getline (cin,itemName);
   itemClass.addItem(itemID,itemName);

   string name;
   cout << "Enter the characters name: ";
   getline (cin, name); // gets entire line entered by end user
   character1.setName(name);// the name string variable is passed to the function setName

   int number;
   cout << "Enter the experience points of the character: ";
   cin >> number;
   cin.ignore(); //this will flush out any dangling newlines on the input buffer.
   character1.setPoints(number);

   string description;
   cout << "Enter a description for the character: ";
   getline (cin,description);
   character1.setDescription(description);

   int location;
   cout << "Enter the location of the character: ";
   cin >> location;
   cin.ignore(); //this will flush out any dangling newlines on the input buffer.
   character1.setLocation(location);

   int healthPoints;
   cout << "Enter the health points of the character: ";
   cin >> healthPoints;
   cin.ignore(); //this will flush out any dangling newlines on the input buffer.
   character1.setHealthPoints(healthPoints);

   int attackPoints;
   cout << "Enter the attack points of the character: ";
   cin >> attackPoints;
   cin.ignore(); //this will flush out any dangling newlines on the input buffer.
   character1.setAttackPoints(attackPoints);

/////////////////////////////////////////////////////////////////////////////
// character 2

   playingCharacterClass character2; // creates object called character1

   cout << "Enter the characters name: ";
   getline (cin, name); // gets entire line entered by end user
   character2.setName(name);// the name string variable is passed to the function setName

   cout << "Enter the experience points of the character: ";
   cin >> number;
   cin.ignore(); //this will flush out any dangling newlines on the input buffer.
   character2.setPoints(number);

   cout << "Enter a description for the character: ";
   getline (cin,description);
   character2.setDescription(description);

   cout << "Enter the location of the character: ";
   cin >> location;
   cin.ignore(); //this will flush out any dangling newlines on the input buffer.
   character2.setLocation(location);

   cout << "Enter the health points of the character: ";
   cin >> healthPoints;
   cin.ignore(); //this will flush out any dangling newlines on the input buffer.
   character2.setHealthPoints(healthPoints);

   cout << "Enter the attack points of the character: ";
   cin >> attackPoints;
   cin.ignore(); //this will flush out any dangling newlines on the input buffer.
   character2.setAttackPoints(attackPoints);

   // character 3

   playingCharacterClass character3; // creates object called character1

   cout << "Enter the characters name: ";
   getline (cin, name); // gets entire line entered by end user
   character3.setName(name);// the name string variable is passed to the function setName

   cout << "Enter the experience points of the character: ";
   cin >> number;
   cin.ignore(); //this will flush out any dangling newlines on the input buffer.
   character3.setPoints(number);

   cout << "Enter a description for the character: ";
   getline (cin,description);
   character3.setDescription(description);

   cout << "Enter the location of the character: ";
   cin >> location;
   cin.ignore(); //this will flush out any dangling newlines on the input buffer.
   character3.setLocation(location);

   cout << "Enter the health points of the character: ";
   cin >> healthPoints;
   cin.ignore(); //this will flush out any dangling newlines on the input buffer.
   character3.setHealthPoints(healthPoints);

   cout << "Enter the attack points of the character: ";
   cin >> attackPoints;
   cin.ignore(); //this will flush out any dangling newlines on the input buffer.
   character3.setAttackPoints(attackPoints);


 ///////////////////////////////////////////////////////////////////////////////
   cout << "value in name is " << character1.getName() << endl;
   cout << "value in expPoints is " << character1.getPoints() << endl;
   cout << "Value in description is " << character1.getDescription() <<endl;
   cout << "Value in location is " << character1.getLocation() <<endl;
   cout << "Value in health points is " << character1.getHealthPoints() <<endl;
   cout << "Value in attack points is " << character1.getAttackPoints() <<endl;

   cout << "value in name is " << character2.getName() << endl;
   cout << "value in expPoints is " << character2.getPoints() << endl;
   cout << "Value in description is " << character2.getDescription() <<endl;
   cout << "Value in location is " << character2.getLocation() <<endl;
   cout << "Value in health points is " << character2.getHealthPoints() <<endl;
   cout << "Value in attack points is " << character2.getAttackPoints() <<endl;

   cout << "value in name is " << character3.getName() << endl;
   cout << "value in expPoints is " << character3.getPoints() << endl;
   cout << "Value in description is " << character3.getDescription() <<endl;
   cout << "Value in location is " << character3.getLocation() <<endl;
   cout << "Value in health points is " << character3.getHealthPoints() <<endl;
   cout << "Value in attack points is " << character3.getAttackPoints() <<endl;
 ///////////////////////////////////////////////////////////////////////////////

   system("pause");
}

Recommended Answers

All 6 Replies

with the inventory, the idea is that each character can hold 5 items, so an array, but the array needs to hold the itemID and the itemName therefore an array wont be any good coz it can only hold one or the other.

Is it not working or just polishing? (I haven't read for now whole story).
But as for above, I would suggest structs and unions as best options IMO

OK so i am gonna give up and go for the struct approach, but its not working - i have done it just how i would in C - i am not getting any error codes but when i add an item to the inventory the function is called but it doesnt do anything - here is a simplified version of my code

#include <iostream.h>
#include <string.h>
#define INVENTORYLIMIT 5
using namespace std;

struct itemClass
{
    int itemID;
    string itemName;
};

class characters //name of base class
{
      private:
        int currentLocationID;
        int healthPoints;
        int attackPoints;
        string charName;
        string description;
        itemClass inventory[INVENTORYLIMIT];
        int itemsInInventory;

      public:
        characters(); //A constructor function - must be same name as class itself
        ~characters(); // a destructor function
        int getLocation();
        bool setLocation (int charlocation);
        string getName(); // An accessor function for name
        bool setName(string name); // An mutator function for name
        string getDescription(); // Accessor function for description
        bool setDescription (string chardescription); // A mutator function for description
        bool setHealthPoints(int health); // a mutator function for health points
        int getHealthPoints(); // an accessor function for health points
        bool setAttackPoints(int attack); // a mutator function for attack points
        int getAttackPoints(); // an accessor function for attackpoints
        ///////////////////////////////
        bool addAttackPoints(int newAttackPoints); // a mutator function to subtract attack points
        bool subtractHealthPoints(int subtractHealthPoints); // a mutator function for subtracting Health Points
        bool subtractHitPoints(int subtractHitPoints); // a mutator function for subtracting hit points (attack points)
        //////////////////////////////
        bool addItem(int itemID, string itemName);
        //bool dropItem (int itemID);
        int getinventory();
        int viewallitems();
};
 ////////////////////////////////////////////////////////////////////////////////
class playingCharacterClass: public characters
{
    private:
        int expPoints;
    public:
        bool addExperiencePoints(int points);// a mutator function to add experience points
        bool setPoints(int expPoints); // A mutator function for expPoints
        int getPoints (); // An Accessor for expPoints
};
 ////////////////////////////////////////////////////////////////////////////////
class nonPlayingCharacterClass: public characters
{
    private:
       bool isAlive;
       string speakTip;
};
////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////

bool characters::addItem(int itemID, string itemName)
{
    cout << "hello";
    for(int counter=0;counter<INVENTORYLIMIT;counter++)
    {
        if(inventory[counter].itemID=0)
        {
            inventory[counter].itemID=itemID;
            inventory[counter].itemName=itemName;
            itemsInInventory=itemsInInventory+1;


        }
    }

}


int characters::getinventory()
{
    return itemsInInventory;
}



 /// constructor function // is automatically called when a new object is called
characters::characters()
{
   for(int counter=0;counter<INVENTORYLIMIT;counter++)
   {
       inventory[counter].itemID=0;
   }
   itemsInInventory=0;

   cout << "A character has been born!" << endl;
}



int main(void)
{
   playingCharacterClass character1; // creates object called character1

   
   
int itemID;
   string itemName;
   cout << "Enter the itemID if item to be added to the inventory: ";
   cin >> itemID;
   cin.ignore();
   cout << "Enter the item name: ";
   getline (cin,itemName);
   character1.addItem(itemID, itemName);
   cout << "value in inventory is " << character1.getinventory() << endl;

   
 ///////////////////////////////////////////////////////////////////////////////

   system("pause");
}

Points of correction added as comments

#include <iostream>// Dont use something.h, it is deprecated
#include <string>//see above comment
#include <cstdlib> //MISSING
using namespace std;

#define INVENTORYLIMIT 5

struct itemClass
{
    int itemID;
    string itemName;
};

class characters //name of base class
{
      private:
        int currentLocationID;
        int healthPoints;
        int attackPoints;
        string charName;
        string description;
        itemClass inventory[INVENTORYLIMIT];
        int itemsInInventory;

      public:
        characters(); //A constructor function - must be same name as class itself
        ~characters(); // a destructor function
        int getLocation();
        bool setLocation (int charlocation);
        string getName(); // An accessor function for name
        bool setName(string name); // An mutator function for name
        string getDescription(); // Accessor function for description
        bool setDescription (string chardescription); // A mutator function for description
        bool setHealthPoints(int health); // a mutator function for health points
        int getHealthPoints(); // an accessor function for health points
        bool setAttackPoints(int attack); // a mutator function for attack points
        int getAttackPoints(); // an accessor function for attackpoints
        ///////////////////////////////
        bool addAttackPoints(int newAttackPoints); // a mutator function to subtract attack points
        bool subtractHealthPoints(int subtractHealthPoints); // a mutator function for subtracting Health Points
        bool subtractHitPoints(int subtractHitPoints); // a mutator function for subtracting hit points (attack points)
        //////////////////////////////
        bool addItem(int itemID, string itemName);
        //bool dropItem (int itemID);
        int getinventory();
        int viewallitems();
};
 ////////////////////////////////////////////////////////////////////////////////
class playingCharacterClass: public characters
{
    private:
        int expPoints;
    public:
        bool addExperiencePoints(int points);// a mutator function to add experience points
        bool setPoints(int expPoints); // A mutator function for expPoints
        int getPoints (); // An Accessor for expPoints
};
 ////////////////////////////////////////////////////////////////////////////////
class nonPlayingCharacterClass: public characters
{
    private:
       bool isAlive;
       string speakTip;
};
////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////

bool characters::addItem(int itemID, string itemName)
{
    cout << "hello";
    for(int counter=0;counter<INVENTORYLIMIT;counter++)
    {
        if(inventory[counter].itemID==0) //MISSING ´=´, 
        {
            inventory[counter].itemID=itemID;
            inventory[counter].itemName=itemName;
            itemsInInventory=itemsInInventory+1;


        }
    }
	//ADD RETURN VALUE HERE
	return true;
}


int characters::getinventory()
{
    return itemsInInventory;
}



 /// constructor function // is automatically called when a new object is called
characters::characters()
{
   for(int counter=0;counter<INVENTORYLIMIT;counter++)
   {
       inventory[counter].itemID=0;
   }
   itemsInInventory=0;

   cout << "A character has been born!" << endl;
}



int main(void)
{
   playingCharacterClass character1; // creates object called character1

   
   
int itemID;
   string itemName;
   cout << "Enter the itemID if item to be added to the inventory: ";
   cin >> itemID;
   cin.ignore();
   cout << "Enter the item name: ";
   getline (cin,itemName);
   character1.addItem(itemID, itemName);
   cout << "value in inventory is " << character1.getinventory() << endl;

   
 ///////////////////////////////////////////////////////////////////////////////

   system("pause");
}

Points of correction added as comments

#include <iostream>// Dont use something.h, it is deprecated
#include <string>//see above comment
#include <cstdlib> //MISSING
using namespace std;

#define INVENTORYLIMIT 5

struct itemClass
{
    int itemID;
    string itemName;
};

class characters //name of base class
{
      private:
        int currentLocationID;
        int healthPoints;
        int attackPoints;
        string charName;
        string description;
        itemClass inventory[INVENTORYLIMIT];
        int itemsInInventory;

      public:
        characters(); //A constructor function - must be same name as class itself
        ~characters(); // a destructor function
        int getLocation();
        bool setLocation (int charlocation);
        string getName(); // An accessor function for name
        bool setName(string name); // An mutator function for name
        string getDescription(); // Accessor function for description
        bool setDescription (string chardescription); // A mutator function for description
        bool setHealthPoints(int health); // a mutator function for health points
        int getHealthPoints(); // an accessor function for health points
        bool setAttackPoints(int attack); // a mutator function for attack points
        int getAttackPoints(); // an accessor function for attackpoints
        ///////////////////////////////
        bool addAttackPoints(int newAttackPoints); // a mutator function to subtract attack points
        bool subtractHealthPoints(int subtractHealthPoints); // a mutator function for subtracting Health Points
        bool subtractHitPoints(int subtractHitPoints); // a mutator function for subtracting hit points (attack points)
        //////////////////////////////
        bool addItem(int itemID, string itemName);
        //bool dropItem (int itemID);
        int getinventory();
        int viewallitems();
};
 ////////////////////////////////////////////////////////////////////////////////
class playingCharacterClass: public characters
{
    private:
        int expPoints;
    public:
        bool addExperiencePoints(int points);// a mutator function to add experience points
        bool setPoints(int expPoints); // A mutator function for expPoints
        int getPoints (); // An Accessor for expPoints
};
 ////////////////////////////////////////////////////////////////////////////////
class nonPlayingCharacterClass: public characters
{
    private:
       bool isAlive;
       string speakTip;
};
////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////

bool characters::addItem(int itemID, string itemName)
{
    cout << "hello";
    for(int counter=0;counter<INVENTORYLIMIT;counter++)
    {
        if(inventory[counter].itemID==0) //MISSING ´=´, 
        {
            inventory[counter].itemID=itemID;
            inventory[counter].itemName=itemName;
            itemsInInventory=itemsInInventory+1;


        }
    }
	//ADD RETURN VALUE HERE
	return true;
}


int characters::getinventory()
{
    return itemsInInventory;
}



 /// constructor function // is automatically called when a new object is called
characters::characters()
{
   for(int counter=0;counter<INVENTORYLIMIT;counter++)
   {
       inventory[counter].itemID=0;
   }
   itemsInInventory=0;

   cout << "A character has been born!" << endl;
}



int main(void)
{
   playingCharacterClass character1; // creates object called character1

   
   
int itemID;
   string itemName;
   cout << "Enter the itemID if item to be added to the inventory: ";
   cin >> itemID;
   cin.ignore();
   cout << "Enter the item name: ";
   getline (cin,itemName);
   character1.addItem(itemID, itemName);
   cout << "value in inventory is " << character1.getinventory() << endl;

   
 ///////////////////////////////////////////////////////////////////////////////

   system("pause");
}

Oh fantastic, thanks so much - im always missing the little things, and the things i know, quite embarrassing really lol but i guess i only just started with c++ so im hoping to stop making the silly little mistakes quite soon!

Thanks again!!!

Emily

Oh fantastic, thanks so much - im always missing the little things, and the things i know, quite embarrassing really lol but i guess i only just started with c++ so im hoping to stop making the silly little mistakes quite soon!

Thanks again!!!

Emily

Glad I helped!

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.