I'm having troubles with this program I made. Put it simply, it's a basic program using Queues. Whenever I try to input a value, my compiler(Dev C) suddenly stops working. The .exe file crashes and I've no way on how I can execute my program.
#include<iostream>
usingnamespace std;
int *queue;
int rear, front, queueSize;
void enqueue();
void dequeue();
void display();
int main()
{
int choice;
int element;
cout<<"Enter element number: "; cin>>element;
system("cls");
while(choice!=4)
{
cout<<"****************"<<endl;
cout<<"[1] - Enqueue" << endl << "[2] - Dequeue" << endl << "[3] - Display" << endl << "[4] - Exit" << endl;
cout<<"-----------------"<<endl;
cin>>choice;
switch(choice)
{
case 1:
enqueue();
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
break;
}
}
cin.get();
}
void enqueue()
{
if (rear == (queueSize-1))
{
cout<<"\n Overflow! No space left in Queue. Cannot push.";
}
else
{
rear++;
cout<<"\n Enter the element to be pushed in Queue : ";
cin>>queue[rear];
}
}
void dequeue()
{
if (front == rear)
{
cout<<"\n Underflow! Cannot pop any items because Queue is empty.";
}
else
{
cout<<"\n The element that was popped was : "<<queue[++front];
}
}
void display()
{
if (front == rear)
{
cout<<"\n Queue is empty, no element to display";
}
else
{
for(int i = (front+1); i<=rear; i++)
{
cout<<queue[i];
if (i != rear)
{
cout<<"<--";
}
}
}
}
Any ideas on how I can fix this? Any help would be much appreciated.
1. You did not declare a queue ADT(abstract data type) or memory for queue
2. Line 41 will always be false because you never gave those variables a value (line 8)
3. Line 50, queue is a pointer (line 3) by itself, but you are trying to access it like it is pointing to a chunk of memory
The first thing I will recommend fixing is to actually create a new queue by first allocating memory for it (int * queue = newint[SIZE];), then give all those variables on line 8 a value