C++ Potion Store

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

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

#1 sydewayzlocc  Icon User is offline

  • D.I.C Head

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

C++ Potion Store

Posted 01 February 2013 - 02:03 AM

This assignment is as follows :

CS260 Lab: Shelf Life

Instructions

Your RPG game includes all sorts of valuable items, including different types of potions. Normally players find potions on quests or in random places in the game. However, some players just want to buy potions at a shop. In order to support this you have decided to implement an apothecary shop. The realm will support any number of apothecary shops but each one will be implemented with the same class. These shops are "self-service", non-manned kiosks throughout the realm. Players can walk up to a kiosk and either buy a potion (if there are any there) or submit a potion request. Every once in a while the magician comes by the kiosk and services the potion requests. He (or she) makes the potions from the requests and puts them all on the shelf for future players to buy. (Think of it as Redbox for potions).

Receiving orders

Apothecaries don't get to make any potion they want. Instead, they receive orders for potions. Each order is really a request to make a particular type of potion. Requests are processed in the order that they are recevied. Note, though, that just taking orders is not the same as making potions (see next section).

Making potions

From time to time the magician shows up at a kiosk and makes potions for all of the requests. As each potion is made, it is put on a shelf, at the front. Since it's put at the front of the shelf it pushes all of the other potions back. This means that the most recently made potion is at the front of the shelf.

Buying potions

Players that want to buy potions go to the shop and buy a potion. They cannot select which potion they get, it is just the potion at the front of the shelf.

Capacity limits

Finally, each shop has a size limit for the number of orders they can take (the size of the queue) and the number of potions that they can store on their shelf (the size of the stack). There is no restriction on what these limits are (e.g., one shop may be able to handle only 5 orders while another can store 5000 orders) but once a shop is created, the limits for that shop can't change.

Implementation Requirements

Before understanding the implementation requirements you should look at a couple of files: main.h and main.cpp. The header file contains a simple enumerated type of all of the different potion types that the program supports. It also contains the function prototype for a utility function for priting out the potion type as a string. The main.cpp file contains test code to "run" a shop. This code creates an Apothecary object (with the two limits), issues various order requests, tells the shop to make potions a few times, and buys some potions. If you look at this file you will see the different classes, constructors, and methods that are needed by your program.

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.

The Apothecary class

The Apothecary class is where the majority of the work is done. As explained in the overview, each apothecary has different limitations on the number of orders and number of made potions they can store. To support this, the Apothecary class needs to have a constructor that takes in those two values. Next, this class needs to contain a queue for taking orders (order queue) and a stack for holding the completed potions (shelf stack).

In order to take orders, make potions, and allow users to buy potions, the class needs to have these three methods:

bool OrderPotion(PotionType potion);
int MakePotions();
bool BuyPotion(Potion& potion);
The OrderPotion() method takes in a potion type and adds a request for this type of potion to the order queue in the shop. This method needs to make sure that the order queue length limit isn't exceeded. If there is not room to add the request to the queue, the method needs to print a message (see the sample output for the message). The method should return true if the order was successfully added to the queue, false otherwise.

The MakePotions() method reads orders off of the request queue and creates the requested potion. As each potion is made it is put onto the shelf stack. The method returns and integer which is the number of potions made. The method will try to process all of the orders on the request queue. However, MakePotions() needs to be aware of the shelf limit and not make any potions that would put it over the limit. This means that there might be cases where not all of the orders can be processed. In that case there will still be orders left on the queue. Appropriate messages should be printed when potions are made or orders can't be fulfilled. See the sample output.

The BuyPotion() method is the method that is used to take one potion off of the shelf (popping off of the stack). The potion is passed back through a reference parameter. The boolean return value for BuyPotion() is true if a potion was bought and false otherwise.




This is what I have implemented so far and I am getting a host of errors.

main.cpp :

#include <iostream>
#include "apothecary.h"

#ifdef _WIN32
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
#endif

using namespace std;

char* PotionTypeString(PotionType type)
{
	const char* s = "";

	switch (type) {
	case SPEED:
		s =  "Speed";
		break;
	case STRENGTH:
		s =  "Strength";
		break;
	case HEALTH:
		s = "Health";
		break;
	case WISDOM:
		s = "Wisdom";
		break;
	}
	return(s);
}


void BuyPotion(Apothecary& apo)
{
	Potion potion;
	if (apo.BuyPotion(potion)) {
		cout << "Congratulations! You just bought a " << PotionTypeString(potion.GetType()) << " potion!" << endl;
		cout << potion;
	} else {
		cout << "There were no potions available." << endl;
	}
}

void OrderPotion(Apothecary& apo,PotionType type)
{
	bool ret = apo.OrderPotion(type);
	if (ret) {
		cout << "Your potion (" << PotionTypeString(type) << ") has been added to the queue!" << endl;
	} else {
		cout << "The order queue is full." << endl;
	}

}

void MakePotions(Apothecary& apo)
{
	cout << "About to try to make some potions." << endl;
	int count = apo.MakePotions();
	cout << "Made " << count << " potions." << endl;
}

void TestApothecary()
{
	Apothecary apo(5,20);  // order limit, shelf limit

	OrderPotion(apo,WISDOM);
	OrderPotion(apo,WISDOM);
	OrderPotion(apo,SPEED);

	MakePotions(apo);

	OrderPotion(apo,STRENGTH);
	OrderPotion(apo,HEALTH);
	OrderPotion(apo,HEALTH);
	OrderPotion(apo,WISDOM);
	OrderPotion(apo,STRENGTH);
	OrderPotion(apo,HEALTH);

	BuyPotion(apo);
	BuyPotion(apo);
	BuyPotion(apo);
	BuyPotion(apo);

	MakePotions(apo);

	OrderPotion(apo,STRENGTH);
	OrderPotion(apo,STRENGTH);
	OrderPotion(apo,STRENGTH);
	OrderPotion(apo,STRENGTH);
	OrderPotion(apo,STRENGTH);

	MakePotions(apo);

	OrderPotion(apo,HEALTH);
	OrderPotion(apo,WISDOM);
	OrderPotion(apo,HEALTH);
	OrderPotion(apo,WISDOM);
	OrderPotion(apo,SPEED);

	MakePotions(apo);

	OrderPotion(apo,HEALTH);
	OrderPotion(apo,HEALTH);
	OrderPotion(apo,HEALTH);
	OrderPotion(apo,HEALTH);
	OrderPotion(apo,HEALTH);

	MakePotions(apo);

	OrderPotion(apo,HEALTH);
	OrderPotion(apo,WISDOM);
	OrderPotion(apo,HEALTH);
	OrderPotion(apo,WISDOM);
	OrderPotion(apo,SPEED);

	MakePotions(apo);

	BuyPotion(apo);

	MakePotions(apo);

}

int main() {

	TestApothecary();

#ifdef _WIN32
	if (_CrtDumpMemoryLeaks()) {
		cout << "Memory leaks!" << endl;
	}
#endif

	return 0;
}




apothecary.cpp :


#include "apothecary.h"
#include <iostream>
#include <cassert>

using namespace std;

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

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

	nOrders = 0;
	nPotions = 0;
}

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

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

/*void queue::Display()
{
	node * p = new node;
	p = front;

	if(front == NULL)
	{
		cout << "Nothing to display!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << endl;
	}
	else
	{
		cout << "worked" << endl;
	}
}*/



potion.cpp :

#include "Potion.h"
#include <iostream>

using namespace std;
#pragma warning(disable:4996) //allow strcpy

Potion::Potion():potionType(NULL)
{}

/*Potion::~Potion()
{

}*/

Potion::Potion(PotionType potion)
{
	SetType(PotionTypeString(potion));
}

const Potion& Potion::operator=(const Potion& student)
{
	//if it is a self copy, don't do anything
	if(this == &student)
		return *this;
	//make current object *this a copy of the passed in student
	else
	{
		SetType(student.potionType);
		return *this;
	}
}

void Potion::SetType(char* type)
{
	//set new type
	this->potionType = new char[strlen(type)+1];
	strcpy(this->potionType, type);
}

const char* const Potion::GetType()
{
	return potionType;
}




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 orders, int potions);
	bool OrderPotion(PotionType potion);
	int MakePotions();
};

#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;

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

	node * front;
	node * rear;

};
#endif



main.h :

#pragma once
enum PotionType {UNKNOWN, SPEED, STRENGTH, HEALTH, WISDOM};
char* PotionTypeString(PotionType type);



potion.h:

#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




ERRORS :

/media/8c51a03d-e51b-4391-bab9-90222b696438/School/CS260/lab2/main.cpp||In function ‘char* PotionTypeString(PotionType)’:|
/media/8c51a03d-e51b-4391-bab9-90222b696438/School/CS260/lab2/main.cpp|16|warning: enumeration value ‘UNKNOWN’ not handled in switch [-Wswitch]|
/media/8c51a03d-e51b-4391-bab9-90222b696438/School/CS260/lab2/main.cpp|30|error: invalid conversion from ‘const char*’ to ‘char*’ [-fpermissive]|
/media/8c51a03d-e51b-4391-bab9-90222b696438/School/CS260/lab2/main.cpp||In function ‘void BuyPotion(Apothecary&)’:|
/media/8c51a03d-e51b-4391-bab9-90222b696438/School/CS260/lab2/main.cpp|37|error: ‘class Apothecary’ has no member named ‘BuyPotion’|
/media/8c51a03d-e51b-4391-bab9-90222b696438/School/CS260/lab2/main.cpp|38|error: cannot convert ‘const char*’ to ‘PotionType’ for argument ‘1’ to ‘char* PotionTypeString(PotionType)’|
/media/8c51a03d-e51b-4391-bab9-90222b696438/School/CS260/lab2/main.cpp|39|error: no match for ‘operator<<’ in ‘std::cout << potion’|
/media/8c51a03d-e51b-4391-bab9-90222b696438/School/CS260/lab2/main.cpp|39|note: candidates are:|
/usr/include/c++/4.6/ostream|110|note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>::__ostream_type& (*)(std::basic_ostream<_CharT, _Traits>::__ostream_type&)) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]|
/usr/include/c++/4.6/ostream|110|note:   no known conversion for argument 1 from ‘Potion’ to ‘std::basic_ostream<char>::__ostream_type& (*)(std::basic_ostream<char>::__ostream_type&) {aka std::basic_ostream<char>& (*)(std::basic_ostream<char>&)}’|
/usr/include/c++/4.6/ostream|119|note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>::__ios_type& (*)(std::basic_ostream<_CharT, _Traits>::__ios_type&)) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>, std::basic_ostream<_CharT, _Traits>::__ios_type = std::basic_ios<char>]|
/usr/include/c++/4.6/ostream|119|note:   no known conversion for argument 1 from ‘Potion’ to ‘std::basic_ostream<char>::__ios_type& (*)(std::basic_ostream<char>::__ios_type&) {aka std::basic_ios<char>& (*)(std::basic_ios<char>&)}’|
/usr/include/c++/4.6/ostream|129|note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(std::ios_base& (*)(std::ios_base&)) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]|
/usr/include/c++/4.6/ostream|129|note:   no known conversion for argument 1 from ‘Potion’ to ‘std::ios_base& (*)(std::ios_base&)’|
/usr/include/c++/4.6/ostream|167|note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long int) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]|
/usr/include/c++/4.6/ostream|167|note:   no known conversion for argument 1 from ‘Potion’ to ‘long int’|
/usr/include/c++/4.6/ostream|171|note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long unsigned int) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]|
/usr/include/c++/4.6/ostream|171|note:   no known conversion for argument 1 from ‘Potion’ to ‘long unsigned int’|
/usr/include/c++/4.6/ostream|175|note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(bool) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]|
/usr/include/c++/4.6/ostream|175|note:   no known conversion for argument 1 from ‘Potion’ to ‘bool’|
/usr/include/c++/4.6/bits/ostream.tcc|93|note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short int) [with _CharT = char, _Traits = std::char_traits<char>]|
/usr/include/c++/4.6/bits/ostream.tcc|93|note:   no known conversion for argument 1 from ‘Potion’ to ‘short int’|
/usr/include/c++/4.6/ostream|182|note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(short unsigned int) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]|
/usr/include/c++/4.6/ostream|182|note:   no known conversion for argument 1 from ‘Potion’ to ‘short unsigned int’|
/usr/include/c++/4.6/bits/ostream.tcc|107|note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(int) [with _CharT = char, _Traits = std::char_traits<char>]|
/usr/include/c++/4.6/bits/ostream.tcc|107|note:   no known conversion for argument 1 from ‘Potion’ to ‘int’|
/usr/include/c++/4.6/ostream|193|note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(unsigned int) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]|
/usr/include/c++/4.6/ostream|193|note:   no known conversion for argument 1 from ‘Potion’ to ‘unsigned int’|
/usr/include/c++/4.6/ostream|202|note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long long int) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]|
/usr/include/c++/4.6/ostream|202|note:   no known conversion for argument 1 from ‘Potion’ to ‘long long int’|
/usr/include/c++/4.6/ostream|206|note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long long unsigned int) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]|
/usr/include/c++/4.6/ostream|206|note:   no known conversion for argument 1 from ‘Potion’ to ‘long long unsigned int’|
/usr/include/c++/4.6/ostream|211|note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(double) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]|
/usr/include/c++/4.6/ostream|211|note:   no known conversion for argument 1 from ‘Potion’ to ‘double’|
/usr/include/c++/4.6/ostream|215|note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(float) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]|
/usr/include/c++/4.6/ostream|215|note:   no known conversion for argument 1 from ‘Potion’ to ‘float’|
/usr/include/c++/4.6/ostream|223|note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long double) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]|
/usr/include/c++/4.6/ostream|223|note:   no known conversion for argument 1 from ‘Potion’ to ‘long double’|
/usr/include/c++/4.6/ostream|227|note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(const void*) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]|
/usr/include/c++/4.6/ostream|227|note:   no known conversion for argument 1 from ‘Potion’ to ‘const void*’|
/usr/include/c++/4.6/bits/ostream.tcc|121|note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>::__streambuf_type*) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__streambuf_type = std::basic_streambuf<char>]|
/usr/include/c++/4.6/bits/ostream.tcc|121|note:   no known conversion for argument 1 from ‘Potion’ to ‘std::basic_ostream<char>::__streambuf_type* {aka std::basic_streambuf<char>*}’|
/usr/include/c++/4.6/bits/basic_string.h|2693|note: template<class _CharT, class _Traits, class _Alloc> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const std::basic_string<_CharT, _Traits, _Alloc>&)|
/usr/include/c++/4.6/ostream|451|note: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, _CharT)|
/usr/include/c++/4.6/ostream|456|note: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, char)|
/usr/include/c++/4.6/ostream|462|note: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, char)|
/usr/include/c++/4.6/ostream|468|note: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, signed char)|
/usr/include/c++/4.6/ostream|473|note: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, unsigned char)|
/usr/include/c++/4.6/ostream|493|note: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const _CharT*)|
/usr/include/c++/4.6/bits/ostream.tcc|323|note: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const char*)|
/usr/include/c++/4.6/ostream|510|note: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const char*)|
/usr/include/c++/4.6/ostream|523|note: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const signed char*)|
/usr/include/c++/4.6/ostream|528|note: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const unsigned char*)|
||=== Build finished: 50 errors, 1 warnings ===|



Please help! Im not sure what these errors are or how to fix them?

Is This A Good Question/Topic? 0
  • +

Replies To: C++ Potion Store

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3616
  • View blog
  • Posts: 11,263
  • Joined: 05-May 12

Re: C++ Potion Store

Posted 01 February 2013 - 06:23 AM

Don't get overwhelmed by the warnings and errors. Read them one at a time and try to fix them.

For example, it is just warning you that you your switch statement at line 16 is not handling the UNKNOWN value. You can probably fix this now or later.

Next it says you have an error where you are trying to convert a const char * to char * on line 30. This is quite true, so you'll have to make decision on what you want to do. I usually recommend std::string's instead of pointers to characters, but it'll depend on your situation.

Anyway, one you fix one error or warning, recompile, and look at the next set of errors that show up.

The problems you are encountering is because you are not compiling and testing your code as you go along. Instead you chose to do a brain dump, and now you have to deal with the results of that.
Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5571
  • View blog
  • Posts: 11,908
  • Joined: 02-June 10

Re: C++ Potion Store

Posted 01 February 2013 - 06:36 AM

This is your homework.

Quote

This is what I have implemented so far and I am getting a host of errors.


Really, that's your total question? So how are we to interpret that? Because it sounds like you're saying "Do my debugging for me."


Quote

Please help! Im not sure what these errors are or how to fix them?


Well read them.

Quote

‘class Apothecary’ has no member named ‘BuyPotion’

How is this hard to understand? You tried to use something called "BuyPotion" and it doesn't exist in the class "Apothecary".
I see "OrderPotion" and "MakePotion" but I don't see "BuyPotion".

Get some sleep. Take your time. Read the errors but don't fear them. Reason it out.

If all else fails talk to your professor because they need to know you're struggling so they can adjust the course. Maybe the prof. needs to teach debugging.
Was This Post Helpful? 1
  • +
  • -

#4 sydewayzlocc  Icon User is offline

  • D.I.C Head

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

Re: C++ Potion Store

Posted 03 February 2013 - 11:04 PM

My errors are stemming from apothecary.h and apothecary.cpp. I am unsure as to what the shelf is and what the queue is. I have tried playing around with the constructor ‘Apothecary::Apothecary()’:| Here is my code

from apothecary.cpp

// default constructor

Apothecary::Apothecary()        :

        orders(NULL),

        shelf(NULL)

{

}



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



I am trying to set the data members to null in the constructor, but am not sure which ones It should be, hence the use of queue and shelf, can someone help me change the lines in the constructor?

I meant the use of orders and shelf not queue and shelf
Was This Post Helpful? 0
  • +
  • -

#5 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 - 12:18 AM

Here are the changes that I have made to apothecary.cpp. I have got it down to a total of 4 errors. I am now stuck on the final function, BuyPotion, in which I don't know the name of the stack in which I have to pop a "potion" off of. Or more specifically, what should node be?



apothecary.cpp :
bool Apothecary::BuyPotion(Potion& potion)

{

        node *shelf;



        shelf = new stack(sizeof(shelf));



        if(shelf->isEmpty())

                return false;

        else

                shelf->pop(potion);

                return true;

}


Was This Post Helpful? 0
  • +
  • -

#6 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1368
  • View blog
  • Posts: 4,735
  • Joined: 19-February 09

Re: C++ Potion Store

Posted 04 February 2013 - 12:19 PM

The Apothecary class is a model/representation of the Apothecary Shop. The shop contains a queue of orders for potions, and a stack representing a shelf of potions. How can you represent/add the order queue and shelf within your class?

C++ has its own queue and stack classes, you are using your own queue class is that correct? What about a stack class?


Here the shelf should already be created in the class. There is probably no need to create the queue and stack dynamically or give them a size on creation.

01	bool Apothecary::BuyPotion(Potion& potion)
03	{
05	        node *shelf;
09	        shelf = new stack(sizeof(shelf));



Also sizeof(shelf) is the size of the pointer nothing to do with a stack.
Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg  Icon User is offline

  • member icon


Reputation: 4133
  • View blog
  • Posts: 12,865
  • Joined: 25-December 09

Re: C++ Potion Store

Posted 04 February 2013 - 12:21 PM

Why are you trying to use dynamic memory allocation for a container that should be dynamic in nature? Your last post has a memory leak, shelf is local to this function, when the function exits the memory allocated for this local variable will be lost.

Your instructions state that your class should have a private queue instance in the class, I don't see this in your class definition. All I see are several int variables, but no queue or stack instance variables.

Are you required to write your own queue and stack classes or can you use the std::queue and std::stack classes?

Is the main.cpp file you posted the same as the one you should have received from your instructor? If not please post a copy of the main() you received from your instructor.

Jim
Was This Post Helpful? 0
  • +
  • -

#8 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 - 05:37 PM

Yes I have a queue class in the header 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



as you can see, my BuyPotion function does not match up with anything in apothecary.h,


BuyPotion from apothecary.cpp:
bool Apothecary::BuyPotion(Potion& potion)

{

        queue::node * shelf;



        shelf = new stack(sizeof(shelf));



        if(shelf->isEmpty())

                return false;

        else

                shelf->pop(potion);

                return true;

}



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



I am not allowed to use std::stack and std::queue. The assignment strictly says no use of STL allowed. My problems are definitely isolated to this BuyPotion function. When I try to compile I get the following errors and I need help re-defining BuyPotion.

Errors:
apothecary.cpp||In member function ‘bool Apothecary::BuyPotion(Potion&)’:|
apothecary.cpp|196|error: expected type-specifier before ‘stack’|
apothecary.cpp|196|error: cannot convert ‘int*’ to ‘queue::node*’ in assignment|
apothecary.cpp|196|error: expected ‘;’ before ‘stack’|
apothecary.cpp|200|error: ‘struct queue::node’ has no member named ‘isEmpty’|
apothecary.cpp|206|error: ‘struct queue::node’ has no member named ‘pop’|
||=== Build finished: 5 errors, 0 warnings ===|



Im confused because stack and shelf do not exist. I think that the queue is MaxOrders and the stack is MaxPotions
but am not sure how to "buy" a potion which is really just popping it off the stack.
Was This Post Helpful? 0
  • +
  • -

#9 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 - 05:48 PM

Here is main.cpp

#include <iostream>
#include "apothecary.h"

#ifdef _WIN32
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
#endif

using namespace std;

char* PotionTypeString(PotionType type)
{
	const char* s = "";

	switch (type) {
	case SPEED:
		s =  "Speed";
		break;
	case STRENGTH:
		s =  "Strength";
		break;
	case HEALTH:
		s = "Health";
		break;
	case WISDOM:
		s = "Wisdom";
		break;
	}
	return(s);
}


void BuyPotion(Apothecary& apo)
{
	Potion potion;
	if (apo.BuyPotion(potion)) {
		cout << "Congratulations! You just bought a " << PotionTypeString(potion.GetType()) << " potion!" << endl;
		cout << potion;
	} else {
		cout << "There were no potions available." << endl;
	}
}

void OrderPotion(Apothecary& apo,PotionType type)
{
	bool ret = apo.OrderPotion(type);
	if (ret) {
		cout << "Your potion (" << PotionTypeString(type) << ") has been added to the queue!" << endl;
	} else {
		cout << "The order queue is full." << endl;
	}

}

void MakePotions(Apothecary& apo)
{
	cout << "About to try to make some potions." << endl;
	int count = apo.MakePotions();
	cout << "Made " << count << " potions." << endl;
}

void TestApothecary()
{
	Apothecary apo(5,20);  // order limit, shelf limit

	OrderPotion(apo,WISDOM);
	OrderPotion(apo,WISDOM);
	OrderPotion(apo,SPEED);

	MakePotions(apo);

	OrderPotion(apo,STRENGTH);
	OrderPotion(apo,HEALTH);
	OrderPotion(apo,HEALTH);
	OrderPotion(apo,WISDOM);
	OrderPotion(apo,STRENGTH);
	OrderPotion(apo,HEALTH);

	BuyPotion(apo);
	BuyPotion(apo);
	BuyPotion(apo);
	BuyPotion(apo);

	MakePotions(apo);

	OrderPotion(apo,STRENGTH);
	OrderPotion(apo,STRENGTH);
	OrderPotion(apo,STRENGTH);
	OrderPotion(apo,STRENGTH);
	OrderPotion(apo,STRENGTH);

	MakePotions(apo);

	OrderPotion(apo,HEALTH);
	OrderPotion(apo,WISDOM);
	OrderPotion(apo,HEALTH);
	OrderPotion(apo,WISDOM);
	OrderPotion(apo,SPEED);

	MakePotions(apo);

	OrderPotion(apo,HEALTH);
	OrderPotion(apo,HEALTH);
	OrderPotion(apo,HEALTH);
	OrderPotion(apo,HEALTH);
	OrderPotion(apo,HEALTH);

	MakePotions(apo);

	OrderPotion(apo,HEALTH);
	OrderPotion(apo,WISDOM);
	OrderPotion(apo,HEALTH);
	OrderPotion(apo,WISDOM);
	OrderPotion(apo,SPEED);

	MakePotions(apo);

	BuyPotion(apo);

	MakePotions(apo);

}

int main() {

	TestApothecary();

#ifdef _WIN32
	if (_CrtDumpMemoryLeaks()) {
		cout << "Memory leaks!" << endl;
	}
#endif

	return 0;
}


Was This Post Helpful? 0
  • +
  • -

#10 jimblumberg  Icon User is offline

  • member icon


Reputation: 4133
  • View blog
  • Posts: 12,865
  • Joined: 25-December 09

Re: C++ Potion Store

Posted 04 February 2013 - 06:00 PM

Is this the main() provided by your instructor? Because your instructions state that OrderPotion(), MakePotions() and BuyPotion() should be member functions of your Appothecary class but the functions in main() are not member functions. And normally when you are given a "test" function you don't modify that function.

Jim

This post has been edited by jimblumberg: 04 February 2013 - 06:02 PM

Was This Post Helpful? 0
  • +
  • -

#11 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 - 06:12 PM

Jim,

This is the main() provided by him. These are member functions in my apothecary class

apothecary.cpp
#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


Was This Post Helpful? 0
  • +
  • -

#12 jimblumberg  Icon User is offline

  • member icon


Reputation: 4133
  • View blog
  • Posts: 12,865
  • Joined: 25-December 09

Re: C++ Potion Store

Posted 04 February 2013 - 06:26 PM

But this:
void MakePotions(Apothecary& apo)
{

and this:
void OrderPotion(Apothecary& apo,PotionType type)
{

Are not class member functions. So what does the main.cpp you supplied even have to do with your assignment? Are you required to modify these functions to be part of your class?

Also your Apothecary class should probably have an instance of your queue class as a private variable.

Quote

apothecary.cpp|196|error: cannot convert ‘int*’ to ‘queue::node*’ in assignment|

Your error messages look fairly easy to locate what type of variable is shelf? Is it a queue::node* or is it an int?

Quote

apothecary.cpp|196|error: expected type-specifier before ‘stack’|

Where have you defined a class with the type name of stack?

Jim
Was This Post Helpful? 0
  • +
  • -

#13 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 - 08:18 PM

The stack is the shelf of potions "stack" is there because i couldnt figure out how to implement BuyPotion

I need the pointer to point to the first item on top of the "shelf" which is a stack
Was This Post Helpful? 0
  • +
  • -

#14 jimblumberg  Icon User is offline

  • member icon


Reputation: 4133
  • View blog
  • Posts: 12,865
  • Joined: 25-December 09

Re: C++ Potion Store

Posted 04 February 2013 - 08:44 PM

And where do you define your stack? Nowhere in your code do you define a class named stack. Remember a "stack" is a container, just like your queue container.

I really think you need to review your documentation about classes.


Jim
Was This Post Helpful? 0
  • +
  • -

#15 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:33 PM

nOrders represents the number of orders of potions, or the queue. nPotions represents the number of potions, or the stack.

here is a link to my whole project : https://compilr.com/...pothecary-shop/
Was This Post Helpful? 0
  • +
  • -

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