Enqueue and Dequeue functions not working in C++ menu

Program is supposed to enqueue doubles into a maximum array size of 6. However, it is not stopping at 5 intakes and also will not dequeue properly. It will simply spit out junk locations when dequeuing (after asking it show the queue) instead of dequeuing proper items in the queue. Or I'll put enqueue two items into the queue, and then ask it to dequeue, and it says "The queue is empty." How can I fix this? Thank you.

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
#pragma once
#include <iostream>
#include <string>
using namespace std;

const int SIZE = 6;

class Queue
{
private:
	int front;
	int rear;
	double items[SIZE];

public:
	Queue() { front = rear = SIZE - 1; }
	~Queue() {}
	bool isFull() { return (rear + 1) % SIZE == front; }
	bool isEmpty() { return (front == rear); }
	void makeEmpty() { front = rear = SIZE - 1; }
	void enqueue(double item)
	{
		rear = ((rear + 1) % SIZE);
		items[rear] = item;
	}
	void dequeue(double& item)
	{
		front = ((front + 1) % SIZE);
		item = items[front];
	}
	void showQueue() {}

};


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
#pragma once
#include <iostream>
#include <string>
#include "Queue.h"
using namespace std;

int main()
{
	Queue queue;
	char selection = 0;
	double items = 0;
	while (selection != toupper('q')) {
		{
			cout << "E - Enqueue\n";
			cout << "D - Dequeue\n";
			cout << "S - Show Queue\n";
			cout << "Q - Quit\n";
			cout << "Enter your choice: ";
			cin >> selection;
		}

		switch (selection)
		{
		case 'E':
		case 'e':
		{
			if (!queue.isFull())
				cout << "Enter an item to enqueue: ";
			cin >> items;
			queue.enqueue(items);
			break;
		}
		case 'D':
		case 'd':
		{
			if (queue.isEmpty())
			{
				cout << "The queue is empty.";
				exit(EXIT_FAILURE);
			}
			else {
				queue.dequeue(items);
				cout << "Item has been dequeued.";
			}
			break;
		}
		case 'S':
		case 's': { cout << "To show queue\n"; }
		{
			while (!queue.isEmpty())
			{
				queue.dequeue(items);
				cout << items << " --> ";
			}
			cout << "NULL\n\n";
		}
		break;
		
		}
	}
}
Last edited on
Hey elsa,

You have the makings of a very clean circular queue here.

To fix it, you need to break the task down much more carefully. That, and draw yourself a queue, complete with the words “first” and “rear” and arrows pointing to stuff. For each of the following operations, change your drawing to see how it works.

To enqueue, should you modify the items[rear] cell before or after updating rear?

To dequeue, do you need to modify any cells?

To showQueue, you need to write your class method. It is a loop. Hint:

  for (int n = front; n != rear; n = (n + 1) % SIZE)

Since this is a member function, you do not need to dequeue anything, or check for emptiness, or stuff like that.

BTW, line 12 should read while (toupper(selection) != 'Q') {.

I would personally also initialize front and rear to zero, but that doesn’t really matter.

[edit] Also, line 11: items represents a single item (a single double). It should be named item, right?

Hope this helps.
Last edited on
Thank you duthomhas, however when I run the following code in the void showQueue()

1
2
3
4
5
	void showQueue() {
		for (int n = front; n != rear; n = (n + 1) % SIZE) {
			cout << n << "NULL\n\n";
	
		}


it prints out junk values ...any way around this? Thanks again.
Do add std::cout statements to observe what your code does while it works.
If I added 9, 8, 7, 6, 5, 4, 3, what should the Queue show?

Just to compare:
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
#include <iostream>


class Queue
{
public:
    Queue(int size_arg);
    ~Queue();
    bool isFull();
    bool isEmpty();
    void makeEmpty();
    void enqueue(double item);
    double dequeue(double& item);
    void showQueue();

private:
    const int size;
    int front;
    int rear;
    double* items;
};


Queue::Queue(int size_arg)
    : size { size_arg }
    , front { size - 1 }
    , rear { size - 1 }
    , items { new double[size] {} }
{
    // Debug - to be deleted:
    std::cout << "--> front is now " << front
              << "; rear is now " << rear << '\n';
}


Queue::~Queue()
{
    delete[] items;
}


bool Queue::isFull()
{
    return (rear + 1) % size == front;
}


bool Queue::isEmpty()
{
    return front == rear;
}


void Queue::makeEmpty()
{
    front = rear = size - 1;
}


void Queue::enqueue(double item)
{
    rear = (rear + 1) % size;
    items[rear] = item;
    // Debug - to be deleted:
    std::cout << "--> front is now " << front
              << "; rear is now " << rear << '\n';
}


double Queue::dequeue(double& item)
{
    front = (front + 1) % size;
    item = items[front];
    // Debug - to be deleted:
    std::cout << "--> front is now " << front
              << "; rear is now " << rear << '\n';
    return item;
}


void Queue::showQueue()
{
    // Debug - to be deleted:
    std::cout << "--> front is now " << front
              << "; rear is now " << rear << '\n';
    for (int n = front; n != rear; n = (n + 1) % size) {
        std::cout << "element " << n << ": " << items[n] << '\n';
    }
}


int main()
{
    Queue queue(6);
    double items {};
    char selection {};
    while (::toupper(selection) != 'Q') {
        {
            std::cout << "E - Enqueue\n"
                         "D - Dequeue\n"
                         "S - Show Queue\n"
                         "Q - Quit\n"
                         "Enter your choice: ";
            std::cin >> selection;
        }

        switch (selection)
        {
        case 'E':
        case 'e':
            if ( !queue.isFull() ) {
                std::cout << "Enter an item to enqueue: ";
            }
            std::cin >> items;
            queue.enqueue(items);
            break;

        case 'D':
        case 'd':
            if (queue.isEmpty()) {
                std::cout << "The queue is empty.\n";
                // Why?
//                std::exit(EXIT_FAILURE);
            }
            else {
                queue.dequeue(items);
                std::cout << "Item has been dequeued.\n";
            }
            break;

        case 'S':
        case 's':
            std::cout << "Showing queue...\n";
            queue.showQueue();
            std::cout << "NULL\n\n";
            break;

        default:
            break;
        }
    }
}


Output:
--> front is now 5; rear is now 5
E - Enqueue
D - Dequeue
S - Show Queue
Q - Quit
Enter your choice: e
Enter an item to enqueue: 9
--> front is now 5; rear is now 0
E - Enqueue
D - Dequeue
S - Show Queue
Q - Quit
Enter your choice: e
Enter an item to enqueue: 8
--> front is now 5; rear is now 1
E - Enqueue
D - Dequeue
S - Show Queue
Q - Quit
Enter your choice: e
Enter an item to enqueue: 7
--> front is now 5; rear is now 2
E - Enqueue
D - Dequeue
S - Show Queue
Q - Quit
Enter your choice: e
Enter an item to enqueue: 6
--> front is now 5; rear is now 3
E - Enqueue
D - Dequeue
S - Show Queue
Q - Quit
Enter your choice: e
Enter an item to enqueue: 5
--> front is now 5; rear is now 4
E - Enqueue
D - Dequeue
S - Show Queue
Q - Quit
Enter your choice: e
4
--> front is now 5; rear is now 5
E - Enqueue
D - Dequeue
S - Show Queue
Q - Quit
Enter your choice: e
Enter an item to enqueue: 3
--> front is now 5; rear is now 0
E - Enqueue
D - Dequeue
S - Show Queue
Q - Quit
Enter your choice: s
Showing queue...
--> front is now 5; rear is now 0
element 5: 4
NULL

Topic archived. No new replies allowed.