mm really annoying

Started by
5 comments, last by dcuk 18 years, 6 months ago
right question is, and really been bugging me how to make the Sell function working so that its able to detail the item object for example array 1, as so far no matter what i do it compiles and links but then freezes, i asked previosuly and followed some advice but to no avail armour, weapon and cItem derived from base Item of note

void Store::Sell(Player & player, int type) {
	Item * mPoint;
	//Item * mPoint1;
	int select=0;
	string cat="Default";
	if(type == 1) { 
		cat = "Weapon";
		mPoint = player.getWeapon();
	}
	if(type == 2) { 
		cat = "Armour"; 
		mPoint = player.getArmor();
	}
	else if(type == 3) { 
		cat = "Item"; 
		mPoint = player.GetItem(1);
			return;
		//mPoint++;
	}
	switch ( type ) {
		case 1: 
			cout <<"\n\t\t" << cat << ": \t\t" << mPoint->GetName() << "\n";
			cout << "\t\tValue is: \t\t" << mPoint->GetValue() << "\n";
			cout << "\t\tDamage Range: \t\t0\n";
			break;
		case 2:
			cout <<"\n\t\t" << cat << ": \t\t" << mPoint->GetName() << "\n";
			cout << "\t\tValue is: \t\t" << mPoint->GetValue() << "\n";
			cout << "\t\tDamage Protection: \t\t0\n";
			break;
		case 3:
			cout <<"\n\t\t" << cat << ": \t\t" << mPoint[0].GetName() << "\n";
			cout << "\t\tValue is: \t" << mPoint->GetValue() << "\n";
			cout << "\t\tQuantity is: \t" << mPoint->GetQty() << "\n";
			cout << "\t\tDesc: \t\t" << mPoint->GetDesc() << "\n";
			return;
...

Implementation of the Player class;

public:
	// Constructor.
	Player();
	~Player() {}

	// Methods
	bool isDead();

	Item * getArmor();
	Item * getWeapon();
	Item * getItem();
	//ADD ITEM

        .....
        ......

	Item * GetWeapon(unsigned int uiIndex)
	{
		if(uiIndex >=0 && uiIndex < wCount)
			return &mWeapon[uiIndex];

		return NULL;
	}
	Item * GetItem(unsigned int uiIndex)
	{
		if(uiIndex >=0 && uiIndex < iCount)
			return &mItem[uiIndex];

		return NULL;
	}
	Item *GetArmour(unsigned int uiIndex)
	{
		if(uiIndex >=0 && uiIndex < aCount)
			return &mArmour[uiIndex];

		return NULL;
	}


	unsigned int GetWCount() { return wCount; }
	unsigned int GetACount() { return aCount; }
	unsigned int GetICount() { return iCount; }

	//Weapon GetWeapon() { return mWeapon; }
	//void race();
	void displayHitPoints();
 
private:
        ...
        .......
	Item *		mWeapon;
	Item *		mArmour;
	Item *		mItem;
	unsigned int wCount;
	unsigned int aCount;
	unsigned int iCount;
	//Armour      mArmor;
	std::string	mRace;
	//Weapon      mWeapon;

and a snippet of initialing in Player's constructor

Player::Player()
{
	aCount = 1;
	mArmour       = new Armour[aCount];
	wCount = 1;
	mWeapon		  =	new Weapon[wCount];
	iCount = 2;
	mItem		  = new cItem[iCount];
	mItem[0] = cItem("Potion",5, 10, "Healing Tonic");
	mItem[1] = cItem("Ether", 3, 40, "Partially restores MagicPoints");


Advertisement
I really have no clue what the problem is. Could you try rewording your problem, or give an example?
Hmm, mostly I'm surprised that this code compiles in the first place. You're calling your player.getWeapon and player.getArmor without passing it an unsigned int, which is the parameter in the function declaration. Also you're attempting to create arrays using a non-constant value, which should be giving you an error.

Aside from those issues one of the main causes of "freezing" can be that your code has hit an infinite loop (any loop that has an exit condition that never occurs).

An example of an infinite loop:
int a = 0;
while(a<10) a = 4;

or:
for(int i=0; i<10; i);

Neither of these will be flagged as errors by the compiler but will cause the program to halt and "freeze".

I'm a bit confused by your question about having the Sell function detail an item. I think your asking how to iterate through an array and print out useful info about each Item object. If so the best approach depends on how your array is set up. Assuming you change your array declaration to have a constant value you can simply use that value to iterate through your array:
I.E. -> for(int i=0; i<TOTAL_ITEMS; i++)
Then simply do your print code for each item (i.e. cout << "bla: " << item.getBla;
Now if your simply partially filling your array such that currentItems<=TOTAL_ITEMS then you could instead keep currentItems updated with the filled length of the array and then do:
for(int i=0; i<currentItems; i++) and simply traverse from start to the end of the used array. This does require more array management as you'll need to add items to the end of the array (or shift items over to make a space for an item inserted into the middle) as well as shifting items back when you delete an item from the middle.
If you don't want to do all that management you can learn to use vectors, which will handle all of that for you, or you can use a less efficient but simpler method:
Simply create an array to hold what you think will be the max amount of items then in your item class declare a boolean variable that marks if the current object is being used or not. Then when you traverse the array from 0 to max you simply do operations on items that have a true boolean value.

Hopefully this information will help you some. It would be best if you could respond with more information about what you're trying to do in specific.
k tghabks for reply, helped a little but the problem is:

the program worked fine if i wanted to display the informationf or one item, but i thought this wasn't practical as in a game you would exoect to have multiple items

so i tried to create an array, but because i inherited from teh Item class, it all lead to implemation of it being harder to the point where i can't see the problem, so the extracts i posted earlier were the declaration of everything that i have done to create the array, but still freezes, yet compiles!?

Im aware that i have two getItem and GetItem along with two of getWeapon, etc. but originally i thought it would return the first array of data back, as this wasnt solving it, tried to return by t he GetItem method that accepts parameter but still to no avail.
how are you using Sell? there's something funny going on in that function, first you use if-else to go through the values of type, and assuming type == 3 you call GetItem() and then return ... but right after that you enter a switch statement that has 3 as a possible value for type... that 3 case will NEVER execute because the program won't get that far in the first place if type == 3
the return was to see where the error was coming, it not intended to be their, but i thought it could be the initilization of mpoint. ignore it, it's definetly the pointer thats causing the freezing, no infinite loops or anything like taht, commenting out the pointer, and previously before getting into this array business the pointers would work fine,

just wanting to scan through the array of items would be far more impressive than only having the one item
The problem is definetly aroudn this part

Player::Player(){	aCount = 1;	mArmour       = new Armour[aCount];	wCount = 2;	mWeapon		  =	new Weapon[1];	iCount = 2;	mItem		  = new cItem[5];	mItem[0] = cItem("Potion",5, 10, "Healing Tonic");	mItem[1] = cItem("Ether", 3, 40, "Partially restores MagicPoints");	mItem[2] = cItem();	cout << mItem[2].GetName();....


as calling that cout function compiles/links but simply freezes, interesting to note that calling mItem[0] can get the default values from the cItem constructor so one array works, just others are ??

This topic is closed to new replies.

Advertisement