I built a program around the concept of queues, while it compiles and allows input and removal of a member, printing ( selection e) causes the program to infinitely loop. I haven't been able to discover why.
If the stream has non-digits in it when it does the cin >> data;,
then (1) that input fails and (2) the stream (cin) goes into error state. All followup attempts to input will fail too, until that error state is cleared.
One should verify the success:
1 2 3 4 5
if ( cin >> data ) {
// use data
} else {
// handle error
}
However, how is the loop supposed to end?
Nothing does break out of the loop, does it?
Besides, why does 'X' dequeue()?
[EDIT]
1 2 3
void Queue:: dequeue()
{
node *temp = new node;
Why a new, unused node there?
Where do you delete nodes?
There are various memory issues - removing a node doesn't free the memory and there's no class destructor to free the allocated memory. Note that this enqueues at the back and dequeues from the front. Perhaps:
#include <iostream>
#include <cctype>
#include <limits>
using std::cout;
using std::cin;
class Queue {
struct Node {
int info {};
struct Node* next {};
Node(int i) : info(i) {}
};
public:
Queue();
~Queue();
// To be provided if needed
Queue(const Queue&) = delete;
Queue& operator=(const Queue&) = delete;
void enqueue();
void dequeue();
void theDisplay();
void isVacant();
void infoBack();
void infoFront();
void elementsTotal();
private:
Node* back {};
Node* front {};
size_t total {};
};
Queue::Queue() {}
Queue::~Queue() {
while (front) {
constauto tmp {front};
front = front->next;
delete tmp;
}
}
void Queue::infoFront() {
if (total == 0)
cout << "No data at the front of this queue - it is not populated\n";
else
cout << front->info << '\n';
}
void Queue::infoBack() {
if (total == 0)
cout << "No data at the back of this queue - it is not populated\n";
else
cout << back->info << '\n';
}
void Queue::elementsTotal() {
if (total > 0)
cout << "There are " << total << " members in the queue\n";
else
cout << "There are no members in the queue\n";
}
void Queue::enqueue() {
int data {};
while ((cout << "Enter the integer number to enqueue: ") && !(cin >> data)) {
std::cout << "Not a number\n";
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
auto temp {new Node(data)};
if (front == nullptr)
front = back = temp;
else {
back->next = temp;
back = temp;
}
++total;
}
void Queue::dequeue() {
if (total == 0)
cout << "The queue is not populated\n";
else {
cout << "Info is " << front->info << '\n';
constauto tmp {front};
front = front->next;
delete tmp;
if (front == nullptr)
back = nullptr;
--total;
}
}
void Queue::isVacant() {
if (total == 0)
cout << " The queue is not populated\n";
else
cout << " The queue is populated\n";
}
void Queue::theDisplay() {
if (total == 0)
cout << "No info available\n";
elsefor (auto p = front; p != nullptr; p = p->next)
cout << '\n' << p->info;
cout << '\n';
}
int main()
{
Queue queue;
char select {};
while (select != 'X') {
cout << "\n ****************************\n";
cout << " ****************************\n";
cout << "Please make your selection\n";
cout << " A. Add to queue\n";
cout << " B. Remove from queue\n";
cout << " C. Print front of queue\n";
cout << " D. Print back of queue\n";
cout << " E. Print queue\n";
cout << " F. Examine queue population\n";
cout << " G. Print number of queue members\n";
cout << " X. Exit App\n\n";
cout << "Option: ";
cin >> select;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
select = static_cast<char>(std::toupper(static_cast<unsignedchar>(select)));
switch (select) {
case'A':
queue.enqueue();
break;
case'B':
queue.dequeue();
break;
case'C':
queue.infoFront();
break;
case'D':
queue.infoBack();
break;
case'E':
queue.theDisplay();
break;
case'F':
queue.isVacant();
break;
case'G':
queue.elementsTotal();
break;
case'X':
//queue.dequeue();
break;
default:
cout << "Invalid entry\n";
break;
}
}
}