Queue not working as intended.

Hi guys. I'm currently doing a queue system and trying to emulate a real life example of a clinic.
Patients press <1> to register their particulars. If it is full system will tell them that queue is full.
Doctor press <2> to see the next patient. If there is no more patients, the system will tell the doctor that queue is empty.
Press <3> to see who is currently in the queue.

I am using an array for this queue example. My array has a size of 4(for now).
I use the variables "first" and "last" as indexes telling me which is the first and last patient in the queue. (Eg. i use it like this: queueArray[first]. if first == 0 then patient in queueArray[0] is the first patient in the queue)

I believe that there is a problem in either my add function or my printAll function. (I have a hunch its the add function since I took 2 days to figure it out). Btw I am using classes.

Here is my current code:

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
#include <iostream>
#include <string>
#include "patient.h"
#include "queue.h"

using namespace std;

// constructor
queue::queue(int maxSize) {
	arraySize = maxSize;
	queueArray = new patient[maxSize]; // the array to contain patient objects
	first = -1; // a index to show who is first
	last = 0;   // a index to show who is last
};

bool queue::isEmpty () {
	if (first == -1 && last == 0) {
		return true;
	}

	else {
		return false;
	}
};

bool queue::isFull (){
	if (first == 0 && last == arraySize) {
		return true;
	}

	else {
		return false;
	}
};

void queue::add (string inputName, string inputId, int inputQueueNumber) {
	// if last is out of bounds
	if (isFull() == true) {
		cout << "Sorry Queue is full!" <<endl;
	}

	else if (isEmpty() == true) {
		// 1st initialize the patient's record
		newPatient.initialize(inputName, inputId, inputQueueNumber);

		first = 0;
		queueArray[first] = newPatient;
	}

	//else if (isFull() == false && last == arraySize) {
	//	// 1st initialize the patient's record
	//	newPatient.initialize(inputName, inputId, inputQueueNumber);

	//	last = 0;
	//	queueArray[last] = newPatient;
	//	last+=1;
	//}

	else if (last >= first) {
		last+=1;

		if (last < arraySize) {
			newPatient.initialize(inputName, inputId, inputQueueNumber);
			queueArray[last] = newPatient;
		}

		else {
			if (first != 0) {
				last = last%arraySize; //reset last index pointer

				newPatient.initialize(inputName, inputId, inputQueueNumber);
				queueArray[last] = newPatient;
			}

			else {
				cout << "Queue is full!" <<endl;
			}
		}
	};

};

void queue::dequeue(void) {
	//// move the first pointer index to the next element as
	//// the current element is not needed anymore
	//first+=1;

	// As long as array is empty then
	// move the first index pointer to the next patient
	if(isEmpty() == false) {
		first+=1;
	}

	//if (first == last) {
	//	// reset index pointers
	//	// indicates that array is empty
	//	first = -1;
	//	last = 0;
	//}

	else {
		cout << "Queue is empty!" <<endl;
	}
};

void queue::printNext(void) {
	if (isEmpty() == true) {
		cout << "There are currently no patients in the queue!" <<endl;
	}

	else if (first >= 0) {
			cout <<endl << queueArray[first];
	}
};

void queue::printAll(void) {
	if (isEmpty() == true) {
		cout << "There are currently no patients in the queue!" <<endl;
	}

	else if (first == last) {
	}
	
	else if (first < last) {
		/*for (int iterator = first; iterator < last; iterator++) {
			cout << queueArray[iterator] <<endl;
		}*/

		int f = first;

		while(f < last) {
			cout << queueArray[f];
			f++;
		}
	}

	else if (first > last) {
		int f = first;
		int l = last;

		while (f < arraySize) {
			cout << queueArray[first];
			f+=1;
		}

		while (l < first) {
			cout << queueArray[l];
			l+=1;
		}
	}

};
Last edited on
and this is my 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
#include <iostream>
#include <string>
#include "patient.h"
#include "queue.h"

using namespace std;

void main (void) {
	const int maxSize = 4; // size of clinic
	queue *clinicQueue = new queue(maxSize); // the clinic

	string inputName, inputId, choice;
	int inputQueueNumber = 1;

	while (true) {
		cout << "Welcome to XYZ Clinic queueing system!" <<endl
			<< "Press <1> to get a queue number" <<endl
			<< "Press <2> to show next patient" <<endl
			<< "Press <3> to show all patients" <<endl
			<< "Press <4> to quit" <<endl
			<<endl;

		getline(cin, choice);

		if (choice == "1"){
			cout << "Input your name: ";
			getline(cin, inputName);
			cout << "Input your id: ";
			getline(cin, inputId);
			clinicQueue->add(inputName, inputId, inputQueueNumber);
			inputQueueNumber++;

		}

		else if (choice == "2"){
			clinicQueue->printNext();
			clinicQueue->dequeue();
		}

		else if (choice == "3") {
			clinicQueue->printAll();
		}

		else if (choice == "4") {
			break;
		}

		else{
			cout << "What?" <<endl;
		}

		system("pause");
		system("cls");
	}

};
Last edited on
One of the problems I encountered is when I pressed <1> and added a patient, then I pressed <3> it doesnt show the patient. But funnily enough when I added 4 patients (pressed <1> 4 times) and then pressed <3> the program shows all the patients.

Also I cant seem to add patients to the front of the array(Circular queue). If lets say queue is full and then I pressed <2> then first patient is now queueArray[1]. When I want to add the next patient to queueArray[0] I used last = last%arraySize to make. But there seems to be some problem..
closed account (DSLq5Di1)
Looks overly complicated.. see my example 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
public:
	bool isEmpty() { return num_of_elements == 0; }
	bool isFull() { return num_of_elements == arraySize; }
	
	void add(...)
	{
		if (!isFull())
		{
			int back = (front + num_of_elements++) % arraySize;			
			queueArray[back] = ...;
		}	
	}
	
        void dequeue()
        {
        	if (!isEmpty())
        	{
        		front = (front + 1) % arraySize;
        		--num_of_elements;
        	}
        }

        void printAll()
        {        	
        	for (int count = 0; count < num_of_elements; ++count)
        	{
        		int i = (front + count) % arraySize;
        		cout << queueArray[i];        	
        	}
        }
private:
	...
	int front, num_of_elements; // initialize to 0 in constructor 

Why does your queue::add(...) construct patients? a patient should be created outside the queue and passed in as an argument. I would also recommend using templates to make your code generic.
Thank you Sir! It works perfectly and your code is so easy to understand! And thanks for the advice on the templates. I will look into it :)
Topic archived. No new replies allowed.