在基于数组的队列中输入元素时程序意外退出并且不继续编译程序的其余部分

Program Exits Unintentionally When Entering Elements in Array-Based Queue and Does not Continue to Compile the Rest of the Program

我遇到了这个错误,我的程序在为基于数组的队列输入元素后意外退出。它应该 运行 像这样:

~ 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.

我所包含的操作队列的方法,例如添加元素、删除、打印队列、计算索引、清除队列。正如我所说,即使我将大小设置为 10,程序也会在输入两个元素时 退出。它不会继续编译。我怀疑这个错误是在 AddToEnd() 方法中的某个地方,它是程序中的第一个构造函数,但我没有得到任何语法错误。让我知道我是否可以提供有关该错误的更多信息。代码如下:

#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;
}

正如 Johnny Mopp 所建议的,您应该重复读取用户选择并处理该选择的过程。最简单的方法是用 while 循环包围相关代码,如下所示:

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;
}

请注意,有些教授不喜欢 while (true) 循环,因此您可能需要发明一个简单的 bool running = true; 标志并将其用作您的条件。将其设置为 false 以导致循环停止重复。