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