簡體   English   中英

在基於數組的隊列中輸入元素時程序意外退出,並且不繼續編譯程序的Rest

[英]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 以使循環停止重復。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM