C++ Potion Store

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • 4

57 Replies - 1908 Views - Last Post: 08 February 2013 - 06:41 PM Rate Topic: -----

#16 jimblumberg  Icon User is online

  • member icon


Reputation: 4290
  • View blog
  • Posts: 13,454
  • Joined: 25-December 09

Re: C++ Potion Store

Posted 04 February 2013 - 09:43 PM

But the stack should be representing not just the number of potions on the shelf but also the type of potions on the shelf. A stack is a container not an int. Also your queue should be representing not just the number of potions on order but also the type of potions that are on order, this is also not an int but a slightly different type of container. See this link for Stack and Queue. You should see similarities between these two containers, they are both data containers for example.

Jim
Was This Post Helpful? 0
  • +
  • -

#17 sydewayzlocc  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 79
  • Joined: 18-April 12

Re: C++ Potion Store

Posted 04 February 2013 - 09:51 PM

So do I need to implement an entire new class for the stack?
Was This Post Helpful? 0
  • +
  • -

#18 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1432
  • View blog
  • Posts: 4,968
  • Joined: 19-February 09

Re: C++ Potion Store

Posted 04 February 2013 - 10:37 PM

View Postsydewayzlocc, on 05 February 2013 - 06:51 AM, said:

So do I need to implement an entire new class for the stack?


The stack is almost the same as the queue. The main difference is in the stack the last object is removed (LIFO) and in a queue the first (oldest) object is removed (FIFO). The stack's functions are usually called push and pop.


I would remove the queue implementation functions from Apothecary.cpp and put them in queue.cpp.

Your main.cpp with its functions looks fine. The functions are similarly named to some of the member functions.
Was This Post Helpful? 0
  • +
  • -

#19 sydewayzlocc  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 79
  • Joined: 18-April 12

Re: C++ Potion Store

Posted 04 February 2013 - 11:14 PM

Ok, so I took the queue member functions out of apothecary.cpp and added them to a separate file queue.cpp


queue.cpp :
#include "apothecary.h"
#include "assert.h"
#include "queue.h"
#include <iomanip>


queue::queue()
{
	front = NULL;
	rear = NULL;
}

queue::queue(const queue& aQueue):front(NULL), rear(NULL)
{
	if(aQueue.front == NULL)
	{
		front = rear = NULL;
	}
	else
	{
		//copy first node
		front = new node;
		assert(front != NULL); //check allocation
		front->item = aQueue.front->item;

		//copy the rest of the queue
		node * destNode = front;				//points to the last node in new queue
		node * srcNode = aQueue.front->next;    //points to node in aQueue
		while(srcNode != NULL) //or while (srcNode)
		{
			destNode->next = new node;
			assert(destNode->next != NULL); //check allocation
			destNode = destNode->next;
			destNode->item = srcNode->item;

			srcNode = srcNode->next;
		}
		destNode->next = NULL;

		//set rear pointr
		rear = destNode;
	}
}
const queue& queue::operator= (const queue& aQueue)
{
	if(this == &aQueue)
		return *this;
	else
	{
		//release dynamically allocated memory held by current object
		node * curr = front;
		while(front)
		{
			curr = front->next;
			delete front;
			front = curr;
		}

		//make a deep copy of aQueue
		if(aQueue.front == NULL)
		{
			front = rear = NULL;
		}
		else
		{
			//copy first node
			front = new node;
			assert(front != NULL); //check allocation
			front->item = aQueue.front->item;

			//copy the rest of the queue
			node * destNode = front;				//points to the last node in new queue
			node * srcNode = aQueue.front->next;    //points to node in aQueue
			while(srcNode != NULL) //or while (srcNode)
			{
				destNode->next = new node;
				assert(destNode->next != NULL); //check allocation
				destNode = destNode->next;
				destNode->item = srcNode->item;

				srcNode = srcNode->next;
			}
			destNode->next = NULL;

			//set rear pointr
			rear = destNode;
		}
		return *this;
	}
}

bool queue::enqueue(const Potion& aPotion)
{
	//add to the rear
	node * newNode = new node;
	newNode->item = aPotion;
	newNode->next = NULL;

	if(front == NULL)
	{
		front = newNode;
	}
	else
	{
		rear->next = newNode;
		rear = newNode;
	}
	//rear = newNode;
	return true;
}

bool queue::dequeue()
{
	//empty stack, has nothing to pop
	if(front == NULL)
	{
		cout << "Queue is empty." << endl;
	}
	else
	{
		node * temp = front;
		if(front == rear) //the only node
			front = rear = NULL;
		else
			front = front->next;

		temp->next = NULL;
		delete temp;
	}
	return true;
}



apothecary.cpp
#include "apothecary.h"
#include "assert.h"
#include "queue.h"
#include <iomanip>

#include <iostream>



using namespace std;



// default constructor

Apothecary::Apothecary():MaxOrders(0), MaxPotions(0), nOrders(0), nPotions(0)
{}

Apothecary::Apothecary(int orders, int potions)
{
	MaxOrders = orders;
	MaxPotions = potions;

	nOrders = 0;
	nPotions = 0;
}



int Apothecary::MakePotions()
{
	queue Qd;

	Qd.dequeue();
    nOrders--;

	return nOrders;
}

bool Apothecary::OrderPotion(PotionType potion)
{
	queue Q;

	if(nOrders < MaxOrders)
	{
		if(Q.enqueue(Potion(potion)))
		{
			nOrders++;
			return true;
		}
		else
		{
			cout << "enq failed" << endl;
			return false;
		}
	}
	else
	{
		cout << "Queue is full!" << endl;
		return false;
	}
}

bool Apothecary::BuyPotion(Potion& potion)

{

        queue * shelf;



        shelf = new stack(sizeof(shelf));



        if(shelf->isEmpty())

                return false;

        else

                shelf->pop(potion);

                return true;

}




My main problem still is with the BuyPotion Function.
Was This Post Helpful? 0
  • +
  • -

#20 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1432
  • View blog
  • Posts: 4,968
  • Joined: 19-February 09

Re: C++ Potion Store

Posted 04 February 2013 - 11:35 PM

Quote

The Potion class

The Potion class contains the details about a potion. Your potion class should have a private data member to store the potion type for a created potion. The Potion class needs to have a GetType() method that returns the potion type for a given potion. You also need a default value constructor. You are free to add any other methods that you need.


Your instructions say that your potion class should have a data member to store the potion type, and GetType() should return a potion type.

The potion type is an enum.

enum PotionType {UNKNOWN, SPEED, STRENGTH, HEALTH, WISDOM};




#ifndef POTION_H
#define POTION_H
#include "main.h"

class Potion
{
private:
	char* potionType;

public:
	Potion();
	Potion(PotionType potion);
	//~Potion();

	void SetType(char* type);
	const char* const GetType();

	const Potion& operator=(const Potion& student);	 //overloading assignment operator
};

#endif




Notice you are incorrectly declaring the potion type as a char*.
Was This Post Helpful? 0
  • +
  • -

#21 sydewayzlocc  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 79
  • Joined: 18-April 12

Re: C++ Potion Store

Posted 04 February 2013 - 11:48 PM

Changed in potion.cpp :

enum Potion::GetType()
{
	return potionType;
}



potion.h :

#ifndef POTION_H
#define POTION_H
#include "main.h"

class Potion
{
private:
	enum potionType;

public:
	Potion();
	Potion(PotionType potion);
	~Potion();

	void SetType(char* type);
	enum GetType();

	const Potion& operator=(const Potion& student);	 //overloading assignment operator
};

#endif



After recompiling I get a couple more errors regarding the change

Errors
potion.h|8|error: use of enum ‘potionType’ without previous declaration|
potion.h|16|error: use of enum ‘GetType’ without previous declaration|
potion.h|16|error: expected unqualified-id before ‘)’ token|
apothecary.cpp||In member function ‘bool Apothecary::BuyPotion(Potion&)’:|
apothecary.cpp|72|error: expected type-specifier before ‘stack’|
apothecary.cpp|72|error: cannot convert ‘int*’ to ‘queue*’ in assignment|
apothecary.cpp|72|error: expected ‘;’ before ‘stack’|
apothecary.cpp|82|error: ‘class queue’ has no member named ‘pop’|
||=== Build finished: 7 errors, 0 warnings ===|


Was This Post Helpful? 0
  • +
  • -

#22 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1432
  • View blog
  • Posts: 4,968
  • Joined: 19-February 09

Re: C++ Potion Store

Posted 05 February 2013 - 12:05 AM

enum is a keyword a bit like class. A enum called PotionType has already been created.


To create a variable of PotionType you can do :

08	    PotionType potionType;


Was This Post Helpful? 0
  • +
  • -

#23 sydewayzlocc  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 79
  • Joined: 18-April 12

Re: C++ Potion Store

Posted 05 February 2013 - 02:48 PM

Ok I declared the variable like this:

potion.h

#ifndef POTION_H
#define POTION_H
#include "main.h"

class Potion
{
private:
	PotionType potionType;

public:
	Potion();
	Potion(PotionType potion);
	~Potion();

	void SetType(char* type);
	enum GetType();

	const Potion& operator=(const Potion& student);	 //overloading assignment operator
};

#endif



Now I am getting an error regarding the GetType function.

potion.h|16|error: use of enum ‘GetType’ without previous declaration|
Was This Post Helpful? 0
  • +
  • -

#24 jimblumberg  Icon User is online

  • member icon


Reputation: 4290
  • View blog
  • Posts: 13,454
  • Joined: 25-December 09

Re: C++ Potion Store

Posted 05 February 2013 - 03:12 PM

The GetType function should be defined as:

PotionType GetType();

This function will be returning a PotionType. PotionType is a type of variable that was defined in main.h.

I also recommend using a PotionType in your SetType() function:

void SetType(PotionType type);



Jim
Was This Post Helpful? 1
  • +
  • -

#25 sydewayzlocc  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 79
  • Joined: 18-April 12

Re: C++ Potion Store

Posted 05 February 2013 - 03:28 PM

Thanks Jim that cleared up that error. Now Im back to figuring out my BuyPotion function. As far as I know, the queue represents orders that have been placed, and the stack should hold the potions that are created by MakePotions(), I read the tutorial on stacks and do I need to implement an array that I point to the top of to be able to pop the potion off of? Im pretty lost with this concept.
Was This Post Helpful? 0
  • +
  • -

#26 jimblumberg  Icon User is online

  • member icon


Reputation: 4290
  • View blog
  • Posts: 13,454
  • Joined: 25-December 09

Re: C++ Potion Store

Posted 05 February 2013 - 03:34 PM

For your BuyPotion() function, if there are some potions on the shelf, you remove a potion from the shelf.

Quote

I read the tutorial on stacks and do I need to implement an array that I point to the top of to be able to pop the potion off of? Im pretty lost with this concept.


How are you creating your stack? Please show the current code for your Apothecary class and the code for both your queue and your stack.

Jim
Was This Post Helpful? 0
  • +
  • -

#27 sydewayzlocc  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 79
  • Joined: 18-April 12

Re: C++ Potion Store

Posted 05 February 2013 - 03:50 PM

I believe that the apothecary class is the "stack" which holds the potions. I am lead to believe this because It has data members nPotions and MaxPotions. My queue class is where the orders for the potions are stored. I thought about in the BuyPotion fucntion doing something like t[nPotions] and pointing to the top? At least that is what I learned from the tutorial, I could be wrong.

apothecary.h :
#ifndef APOTHECARY_H
#define APOTHECARY_H
#include "main.h"
#include "potion.h"
#include "queue.h"

class Apothecary
{
private:
	int MaxOrders;
	int MaxPotions;
	int nOrders;
	int nPotions;

public:
	Apothecary();
	Apothecary(int queuelim, int stacklim);
	bool OrderPotion(PotionType potion);
	int MakePotions();
	bool BuyPotion(Potion& potion);
};

#endif



queue.h:
#ifndef QUEUE_H
#define QUEUE_H
#include "potion.h"
#include "main.h"

class queue
{
public:
	queue();
	queue(const queue& aQueue);
	~queue();

	const queue& operator= (const queue& aQueue);

	bool enqueue(const Potion&);
	bool dequeue();
	void Display();
	bool peek(Potion&)const;
	bool isEmpty(void)const;


	struct node
	{
		Potion item;
		node * next;
	};

	node * front;
	node * rear;

};
#endif



queue.cpp :
#include "apothecary.h"
#include "assert.h"
#include "queue.h"
#include <iomanip>


queue::queue()
{
	front = NULL;
	rear = NULL;
}

queue::queue(const queue& aQueue):front(NULL), rear(NULL)
{
	if(aQueue.front == NULL)
	{
		front = rear = NULL;
	}
	else
	{
		//copy first node
		front = new node;
		assert(front != NULL); //check allocation
		front->item = aQueue.front->item;

		//copy the rest of the queue
		node * destNode = front;				//points to the last node in new queue
		node * srcNode = aQueue.front->next;    //points to node in aQueue
		while(srcNode != NULL) //or while (srcNode)
		{
			destNode->next = new node;
			assert(destNode->next != NULL); //check allocation
			destNode = destNode->next;
			destNode->item = srcNode->item;

			srcNode = srcNode->next;
		}
		destNode->next = NULL;

		//set rear pointr
		rear = destNode;
	}
}
const queue& queue::operator= (const queue& aQueue)
{
	if(this == &aQueue)
		return *this;
	else
	{
		//release dynamically allocated memory held by current object
		node * curr = front;
		while(front)
		{
			curr = front->next;
			delete front;
			front = curr;
		}

		//make a deep copy of aQueue
		if(aQueue.front == NULL)
		{
			front = rear = NULL;
		}
		else
		{
			//copy first node
			front = new node;
			assert(front != NULL); //check allocation
			front->item = aQueue.front->item;

			//copy the rest of the queue
			node * destNode = front;				//points to the last node in new queue
			node * srcNode = aQueue.front->next;    //points to node in aQueue
			while(srcNode != NULL) //or while (srcNode)
			{
				destNode->next = new node;
				assert(destNode->next != NULL); //check allocation
				destNode = destNode->next;
				destNode->item = srcNode->item;

				srcNode = srcNode->next;
			}
			destNode->next = NULL;

			//set rear pointr
			rear = destNode;
		}
		return *this;
	}
}

bool queue::enqueue(const Potion& aPotion)
{
	//add to the rear
	node * newNode = new node;
	newNode->item = aPotion;
	newNode->next = NULL;

	if(front == NULL)
	{
		front = newNode;
	}
	else
	{
		rear->next = newNode;
		rear = newNode;
	}
	//rear = newNode;
	return true;
}

bool queue::dequeue()
{
	//empty stack, has nothing to pop
	if(front == NULL)
	{
		cout << "Queue is empty." << endl;
	}
	else
	{
		node * temp = front;
		if(front == rear) //the only node
			front = rear = NULL;
		else
			front = front->next;

		temp->next = NULL;
		delete temp;
	}
	return true;
}


Was This Post Helpful? 0
  • +
  • -

#28 jimblumberg  Icon User is online

  • member icon


Reputation: 4290
  • View blog
  • Posts: 13,454
  • Joined: 25-December 09

Re: C++ Potion Store

Posted 05 February 2013 - 04:35 PM

Quote

I believe that the apothecary class is the "stack" which holds the potions.

No the Apothecary class is not a stack. A stack is a dynamic data structure, Apothecary is not a dynamic data structure.

Quote

I am lead to believe this because It has data members nPotions and MaxPotions

So what do these variables represent?

Quote

My queue class is where the orders for the potions are stored.

Where is your queue class declared, it should be declared inside your Apothecary class.

The Apothecary class represents the "store" where your potions are stored and the orders are processed. You should have an instance of your queue class in your Apothecary class to hold the orders waiting to be processed. Then you will need an instance of a stack class to hold the information about the potions that are on the shelf.


Jim
Was This Post Helpful? 0
  • +
  • -

#29 sydewayzlocc  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 79
  • Joined: 18-April 12

Re: C++ Potion Store

Posted 05 February 2013 - 04:44 PM

So i need to make a new header to implement my stack of potions, and my queue should go in the apothecary header?
Was This Post Helpful? 0
  • +
  • -

#30 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3663
  • View blog
  • Posts: 11,482
  • Joined: 05-May 12

Re: C++ Potion Store

Posted 05 February 2013 - 04:47 PM

Sort off. The same way you defined and implemented a queue class, you'll need to define and implement a stack class. Then your apothecary can use both the queue and stack classes by including the appropriate headers, and instantiating the appropriate classes.
Was This Post Helpful? 0
  • +
  • -

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • 4