Arrays and Queueing

Having a problem with my code. When I type in a certain size of names I want to be enqueue it shows more names from the txt file than it should. I think I happened to fix this.

Also when I choose to print the list again, it goes in an endless loop.

How can I exactly fix this?
(txt file has like 1000+ names but I'm just using entering small capacity arrays for testing.)

Header and main code below:

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
#ifndef _QueueClass_
#define  _QueueClass_

#include <cstdlib>
#include <string>

class Qqueue
{
public:
	// Constructor
	Qqueue(int cap);
	// Copy Constructor
	Qqueue(const Qqueue& s);
	~Qqueue(); //destructor

	//assignment operator
	void operator=(const Qqueue& s);
	// The member function enqueue: Precondition:  the queue is not full.  If the queue is full, this function signals an error.
	//add value to the end of the queue
	void enqueue(const std::string&  s);


	// The member function dequeue: Precondition:  the queue is not empty. If the queue is empty, this function signals an error.
	// Removes and returns the first item in the queue.
	std::string dequeue();

	// The member function front: Precondition: the queue is not empty.
	std::string&  getfront() const;

	// The member function back: Precondition: the queue is not empty.
	std::string&  getback() const;

	bool IsEmpty() const;
	bool IsFull() const;
	//printing all the elements in the queue
	void print() const;
	int size() const;
	int getCapacity() const;

	//Get the location of DynamicQueue at a specific index
	std::string& getQueue(int);

	//Returns true if the two queues contain exactly the same element values in the same order. Identical in behavior to the == operator.
	bool equals(const Qqueue& q) const;
	// Usage: if (q.equals(q2)) ...

private:
	int Capacity; // Capacity is the maximum number of items that a queue can hold
	std::string* DynamicQueue;
	int num; // How many items are stored in the queue
	int front;
	int back;
};



#endif
#pragma once


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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296

#include "pch.h"

#include <iostream>
#include <fstream>
#include <iomanip>

#include "Qqueue.h" //documenton is in the header file

Qqueue::Qqueue(int CAP)
{
	DynamicQueue = new  std::string[CAP];
	Capacity = CAP;
	front = 0;
	back = -1;
	num = 0;
}

Qqueue::~Qqueue()
{
	delete[]DynamicQueue;
}

Qqueue::Qqueue(const Qqueue& s)
{
	Capacity = s.Capacity;
	front = s.front;
	back = s.back;
	num = s.num;
	DynamicQueue = new std::string[Capacity];
	for (int i = 0; i < Capacity; i++)
	{
		DynamicQueue[i] = s.DynamicQueue[i];
	}
}

void Qqueue::operator=(const Qqueue &s)
{
	Capacity = s.Capacity;
	front = s.front;
	back = s.back;
	num = s.num;
	delete[] DynamicQueue;
	DynamicQueue = new std::string[Capacity];
	for (int i = 0; i < Capacity; i++)
	{
		DynamicQueue[i] = s.DynamicQueue[i];
	}
}

void Qqueue::enqueue(const std::string&  s)
{
	
	// check for queue overflow
	if (IsFull())
	{
		std::cout << "!Error! The queue is full.\n";
		exit(EXIT_FAILURE);
	}

	if (IsEmpty())
	{
		front = 0;
		back = -1;
		num = 0;
	}

	std::cout << "Inserting " <<  s << "\n";

	back++;
	DynamicQueue[back] = s;
	num++;

	//std::cout << "Number of elements: " << num << "\n";

	if (num > 5)
	{
		for (int i = back; i >= back - 4; i--)
		{
			std::cout << "Name: " << DynamicQueue[i] << "\n";
		}
	}
}

std::string Qqueue::dequeue()
{
	if (IsEmpty())
	{
		std::cout << "!Error! The queue is empty.";
		exit(EXIT_FAILURE);
	}

	int number;

	std::cout << "Enter a number: " << "\n";
	std::cin >> number;

	if (num > number)
	{
		for (int i = 0; i < number; i++)
		{
		
		std::cout << "Removing " << DynamicQueue[front] << "\n";
		front++;
		num--;
		}
	}
	else
	{
		front = 0;
		back = -1;
		num = 0;
	}

	std::cout << "Removing " << DynamicQueue[front] << "\n";
	front++;
	num--;
	return DynamicQueue[front - 1];
	
}
std::string& Qqueue::getfront() const
{
	return DynamicQueue[front];
}
std::string& Qqueue::getback() const
{
	return DynamicQueue[back];
}
int Qqueue::size() const
{
	return num;
}
int Qqueue::getCapacity() const
{
	return Capacity;
}
bool Qqueue::IsEmpty() const
{
	return (size() == 0);
}

bool Qqueue::IsFull() const
{
	return (size() == Capacity);
}

bool Qqueue::equals(const Qqueue& q) const
{
	//Returns true if the two queues contain the same elements in the same order.
	if (size() != q.size())
	{
		return false;
	}

	for (int i = 0; i < num; i++)
	{
		if ( DynamicQueue[i] != q.DynamicQueue[i] )
		{
			return false;
		}
	}

	return true;
}
std::string& Qqueue::getQueue(int l)
{
	return DynamicQueue[l];
}
void Qqueue::print() const
{
	int count = 0;

	while (count < num)
	{
		int count = front;
		while (count < num)
		{
			std::cout << DynamicQueue[count] << "\n";
			count++;
		}
	}
}
int main()
{
	std::ifstream file_;
	std::string lastname;

	file_.open("all.last.txt");

	if (file_.fail())
	{
		std::cout << "\nError opening file";
		return 1;
	}

	int userCap;
	std::cout << "\nType the number of of names you want to insert into the queue.";
	std::cout << "\n The max capacity is 500 and the input must be atleast >= 100" << std::endl;
	std::cin >> userCap;

	Qqueue q(userCap);

	for (int i = 0; i < userCap; i++)
	{
		std::string temp;
		file_ >> temp;
		q.enqueue(temp);
	}

	file_.close();

	int choice;
	std::cout << "\nChoice: \n1)Enqueue \n2)Dequeue \n3)Front \n4)Equal \n5)Print \n6)Save \n7)Quit";
	while(1)
	{
		std::cout << "\nEnter your choice...\n";
		std::cin >> choice;
		if (choice == 1)
		{
			std::cout << "\nEnter the name to be enqueued...";
			std::cin >> lastname;
			q.enqueue(lastname);
		}
		else if (choice == 2)
		{
			int number;

			std::cout << "Enter a number: " << "\n";
			std::cin >> number;

			if (number < q.size())
			{
				for (int i = 0; i < number; i++)
				{

					std::cout << "Removing " << q.dequeue() << "\n";
				}
			}
			else
			{
				while (q.IsEmpty() == false)
				{
					q.dequeue();
				}
			}
		}
		else if (choice == 3)
		{
			//print last name in front of the queue
			std::cout << "Front of queue: " << q.getfront() << "\n";
		}
		else if (choice == 4)
		{
			Qqueue q1 = q, q2(userCap);
			q2 = q;

			if (q1.equals(q))
			{
				std::cout << "Copy Constructor working as intended\n";
			}

			if (q2.equals(q))
			{
				std::cout << "Assignment Operator working as intended\n";
			}
		}
		else if (choice ==5)
		{
			q.print();
		}
		else if (choice == 6)
		{
			std::cout << "saving the queue to a file on disk...\n";
			std::ofstream file_new;
			file_new.open("new.last.txt");
			int count = 0;
			while (count < q.size())
			{
				file_new << q.getQueue(count) << "\n";

				count++;
			}
			file_new.close();
		}
		else if (choice == 7)
		{
                        std::cout << "Queue cleared.";
			break;
		}
	}

	//std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
	std::cout << "\n\n Press Enter to Quit: ";
	std::cin.get();
	return 0;
}


Link to the txt file I'm using
http://m.uploadedit.com/bbtc/1560553150325.txt
Last edited on
what your code really says and does in print:
1
2
3
4
5
6
7
8
9
10
11
	int whaa = 0;

	while (whaa < num)  //where does this change..?? how do you get out?!
	{
		int count = front;
		while (count < num)
		{
			std::cout << DynamicQueue[count] << "\n";
			count++;
		}
	}


this is because you ignored common sense and made 2 variables with the same name at 2 scopes. If that code segment had been bigger, it would have taken a *while* to fix that. Do not DO that, use different names for different variables so you can see problems.
Last edited on
I commented out the whole printing function and it still prints names endlessly.
Guessing it has something to do with my main *while*???
I just don't know why it's doing that.
Last edited on
what exact input are you typing into it?
The only inputs I'm doing are:

1) whatever number I want to specify the size of the array
2) Then the menu comes up so I have options to do enqueue, dequeue,save,print,etc...
I press 5 and it prints the names endlessy. Normally hitting 5 should execute the print function and display the names I have in the queue.

Well I think I figured the problem.
Im using visual studio and for some reason when I make changes to it and run, it didnt recompile it for some reason. So any changes I make is mute. Never had that happen to me before.
Guess I'll just edit it with notepad++ and compile it in unix.

Edit: Turns out I didnt even need that extra while loop at all. It works now.
Last edited on
IDEs designed for large projects only recompile when they detect a change, unless you hit rebuild all or whatever that is called. Unix tools can do it too, unless doing a commandline build all every time which won't work on huge projects. Its an intentional feature, but sometimes you forget to save or the file that changed is included but not in the project or various other things can cause it. unix has a touch command to solve this problem as well.
Topic archived. No new replies allowed.