I am having this bug where my program unexpectedly exits after entering elements for my array-based queue. It is supposed to run like so:
~ User enters the number of elements they want in the queue (in this case, the size of the array).
~ After user enters their elements, a menu with the methods' corresponding number should pop up.
~ User picks the number corresponding to what method they want to run, then that specific
method executes.
~ Then the program is supposed to exit.
The methods that I have included manipulate the queue such as adding elements, deletion, printing the queue, counting the index, clearing the queue. As I said, the program exits only when two elements are entered even though I set the size to ten. It does not continue compilation. I suspect that the bug is somewhere in the AddToEnd() method, which is the first constructor in the program, but I do not get any syntax errors whatsoever. Let me know if I could provide more information in relation to the bug. Code is below:
#include <iostream>
using namespace std;
// AddToEnd - at the end of the queue
// DeleteFront - delete element from the front of the queues
// ShowQueue - show all elements of queue
// CountElements - print number of elements
// Clear - initializes the queue
int queue[10], front = -1, back = -1, limit, item;
void AddToEnd () {
cout << "Enter element to add:\n";
cin >> item;
// variable for the element to add
int add = limit - 1;
// if the back = add, queue is filled
// otherwise set the front to 0 and increment the back
if (back == add) {
cout << "queue is filled up" << endl;
} else {
if (front == -1 && back == -1) {
front = 0;
}
back ;
queue[back] = item;
}
}
void DeleteFront () {
if (front == -1) {
cout << "queue is empty\n";
} else {
// item is chosen and front is incremented or set to 0
item = queue[front];
if (front == back) {
back--;
front--;
} else {
front ;
}
}
}
void ShowQueue () {
cout << "Here are the elements in the queue:\n";
for (int i = front; i<= back; i ) {
cout << queue[i] << " " << endl;
}
}
void CountElements () {
if(front == -1) {
cout << "index is zero" << endl;
} else {
int index = 0;
for (int i = front; i <= back; i ) {
index ;
cout << "Index contains " << index << " elements." << endl;
}
}
}
void Clear () {
// iterate through array and set index to 0
for (int i = 0; i < limit; i ) {
queue[i] = 0;
}
}
int main () {
int UserChoice = 0;
cout << "Enter the size of the queue (max is 10 elements)." << endl;
cin >> limit;
cout << "\n1. ADD\n 2. DELETE\n 3. SHOW THE QUEUE\n 4. COUNT THE ELEMENTS\n 5. CLEAR\n";
cin >> UserChoice;
switch (UserChoice) {
case 1: AddToEnd();
break;
case 2: DeleteFront();
break;
case 3: ShowQueue();
break;
case 4: CountElements();
break;
case 5: Clear();
break;
default: cout << "ERROR: Not an option." << endl;
break;
}
cin >> UserChoice;
return 0;
}
CodePudding user response:
As Johnny Mopp suggested, you should repeat the process of reading a user choice and handling that choice. The easiest way to do that is to surround the relevant code with a while
loop, as shown below:
int main () {
int UserChoice = 0;
cout << "Enter the size of the queue (max is 10 elements)." << endl;
cin >> limit;
cout << "\n1. ADD\n 2. DELETE\n 3. SHOW THE QUEUE\n 4. COUNT THE ELEMENTS\n 5. CLEAR\n";
// Add an "infinite" loop.
// You can exit this loop with a break statement,
// return statement, or exit() function call
// instead of using a condition.
while (true) { // <-- Begin loop
cin >> UserChoice;
switch (UserChoice) {
case 1: AddToEnd();
break;
case 2: DeleteFront();
break;
case 3: ShowQueue();
break;
case 4: CountElements();
break;
case 5: Clear();
break;
// Add a case 6 as you mentioned in your comment
// that calls exit(0), so you can get out of your
// while loop.
default: cout << "ERROR: Not an option." << endl;
break;
}
// Since this will run at the top of the loop,
// you don't need it here.
// cin >> UserChoice;
} // <-- End loop
return 0;
}
Note that some professors don't like while (true)
loops, so you may need to invent a simple bool running = true;
flag and use it as your condition. Set it to false to cause the loop to stop repeating.