Enqueue problem!!

Hey guys,

This is a school project that I am starting to work at. I cannot change the main.cpp and the main.h files.

The program is supposed to generally add a Potion Type to a Queue. The potion types are listed in the main.h file as an enum type. The Apothecary class is setting Potion information and queue declarations.

My program runs just fine, but it won't add anything to the queue. I have checked my enqueue funtion a million times and I believe I am spot on. (Maybe it's the need for sleep, hehe).

Below is my entire code so far. Thank you in advance.

Main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#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)
{
	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;
}



Main.h
1
2
3
#pragma once
enum PotionType {UNKNOWN, SPEED, STRENGTH, HEALTH, WISDOM};
char* PotionTypeString(PotionType type);


Potion.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#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 



Potion.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#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 



Apothecary.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#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;
	}
}*/



Queue.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#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 


Thank you so much in advance for reading this ton of code.
The problem is in the MakePotions and OrderPotion functions. Just to start you thinking, what will Q (OrderPotion, line 158) hold when you call OrderPotion the first time?

What will it hold when you call the function a second time? Remember that this is a completely new instance of the function.
uhmm ok. So would I have to dynamically allocate Q?

So, something like this?

queue * Q = new queue;

and then go ahead and use Q.

Thanks.
Something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#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;
        Queue queue ;  // with a better name. 

public:
	Apothecary();
	Apothecary(int orders, int potions);
	bool OrderPotion(PotionType potion);
	int MakePotions();
};
So I have tried the dynamically allocation of Q so, queue * Q = new queue at line 158.
And
I have tried the Queue queue instance in Apothecary.h, but both with no successful results.(With this, I'm just adding the Queue queue instance in the .h file and then going to my OrderPotion function and using it like:

queue.enqueue(Potion(potion))

Am I doing this right?

Anything else I can try?

Thank you!
It's hard for anyone to tell what you actually did if you don't post the changes you made to your code. You don't say why your results aren't successful. Do you receive an error message from the compiler? Does the program just not behave as you expect it to? Supply enough information for people to help you.

If you make the change I suggested above you need to modify Apothecary::OrderPotion and Apothecary::MakePotions:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
int Apothecary::MakePotions()
{                               // make more sense here to return the type of potion made?
    if ( queue.dequeue() )
	    return --nOrders ;      // return the number of orders left in the queue..?
    else
        return 0 ;
}

bool Apothecary::OrderPotion(PotionType potion)
{
	if(nOrders < MaxOrders)
	{
		if(queue.enqueue(potion))
		{
			nOrders++;
			return true;
		}
		else
		{
			cout << "enq failed" << endl;
			return false;
		}
	}
	else
	{
		cout << "Queue is full!" << endl;
		return false;
	}
}


It's entirely possible that this will result in further bugs in your code being exposed as you've never had more than one item in a queue before.

Last edited on
Don't you need a queue.cpp? And I don't see a corresponding BuyPotion function.
@psyclone: the queue member functions are implemented in Apothecary.cpp and for this problem BuyPotion isn't relevant
Topic archived. No new replies allowed.