Clearing elements from a Queue

My program is basically reading a list of names from a txt file and inserting them into the queue and I have several options after that.

My problem is for for pressing quit I have to clear the queue and exit the program. I believe it works (if I did it right) after printing the queue or removing a name from the queue.

However, when I remove something and insert another name back, then try to clear it I get a breaking point.

Main program
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
#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++;

	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;
	}
	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)
	{
		
		while (count < num)
		{
			std::cout << DynamicQueue[count+front] << "\n";
			count++;
		}
	}

	std::cout << "\n" << count << " names have been printed.\n";
}

std::string Qqueue::clearAll()
{
	while (!IsEmpty())
	{
		front = 0;
		back = -1;
		num = 0;
	}

	return DynamicQueue[front];
}

int main()
{
	std::ifstream file_;
	std::string lastname;

	file_.open("all.last.txt");//opens the file to red

	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++)//enqueues each line specified to the capacity inputted
	{
		std::string temp;
		file_ >> temp;
		q.enqueue(temp);
	}

	file_.close();

	int choice;
	while(1)
	{
		std::cout << "\nChoice: \n1)Enqueue \n2)Dequeue \n3)Front \n4)Equal \n5)Print \n6)Save \n7)Quit\n"; //user input menu
		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)
		{
			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)
		{
			std::cout << "Printing queue...\n";
			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)
		{
			q.clearAll();
			if (q.IsEmpty())
			{
				std::cout << "Queue is cleared.";
			}
			else
			{
				std::cout << "Queue not cleared.";
			}
			break;
		}
	}

	std::cout << "\n\n Press Enter to Quit: ";
	std::cin.get();
	return 0;
}


Header File
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;
	std::string clearAll();
	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 


Errors
1
2
3
4
5
'Qqueue.exe' (Win32): Loaded 'C:\Windows\SysWOW64\vcruntime140d.dll'. Cannot find or open the PDB file.
'Qqueue.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ucrtbased.dll'. Cannot find or open the PDB file.
The thread 0x66e8 has exited with code 0 (0x0).
Critical error detected c0000374
Qqueue.exe has triggered a breakpoint.


When I remove say 5 names and add in a new name, it works fine.
But when I remove 5 names and then add back one of those names removed it gets this.
Last edited on
Did you run the program with your debugger?

The debugger should tell you exactly where it detects the problem, and you should be able to view the values of the variables at the time of the crash.

Without a complete program it is difficult to determine the cause of a crash.
I added the header file in there and the errors I get from the console
Again you need to make sure the build is adding debug symbols and run the program with the debugger.

Please post a sample of your input file.

By the way your operator= should be returning a reference, not "void".

Topic archived. No new replies allowed.