简体   繁体   English

为什么这个静态队列程序不起作用? 它突然以自己的方式结束

[英]Why is this static queue program not working? It ends on its own, abruptly

bool queueIsFull(int rearPointer)
{
    if(rearPointer==9)
        return 1;
    else
        return 0;
}

void PUSH (int queue[], int value, int *frontPointer, int *rearPointer)
{
    *rearPointer++;
    if(*frontPointer==-1)
        *frontPointer=0;
    queue[*rearPointer] = value;
}

bool queueIsEmpty (int frontPointer, int rearPointer)
{
    if(rearPointer==(-1) && frontPointer==(-1))
        return true;
    else
        return false;
}

void POP (int* frontPointer, int* rearPointer, int queue[])
{
    cout << "\n Deleted element is: " << queue[*frontPointer];
    if(*rearPointer==*frontPointer)
    {
        *rearPointer=-1;
        *frontPointer=-1;
    }
    else
        *frontPointer++;
}


int main()
{
    int option, value, queue[10];
    int  rearPointer=-1, frontPointer=-1;
    while(1)
    {
        cout << "\n----------[STATIC QUEUE]--------\nEnter a choice: \n1. PUSH AN ELEMENT\n2. POP AN ELEMENT\n3. EXIT\n";
        cin >> option;
        switch(option)
        {
            case 1: if(!queueIsFull(rearPointer))
                    {
                        cout << "\nEnter a value to push in queue: ";
                        cin >> value;
                        PUSH(queue, value, &frontPointer, &rearPointer);
                    }
                    else
                        cout << "\nqueue is full. Empty the queue elements and try again!";
                    break;

            case 2: if(!queueIsEmpty(frontPointer, rearPointer)) // either of the parameters would do.
                        POP(&frontPointer, &rearPointer, queue); // queue is optional. You only need it to display thte poped value.
                    else
                        cout << "\nqueue is already empty. Enter some elements";
                    break;

            case 3: return 0;
                    break;

            default: cout << "\nWrong choice! Try again.";
                   break;
        }
    }
}

I can't get anywhere and I'm not able to debug the problem because the program gets terminated abruptly when I enter values in the queue! 我无法到达任何地方,我无法调试问题,因为当我在队列中输入值时,程序会突然终止! That's why it is getting difficult to understand where the root problem is! 这就是为什么很难理解根本问题在哪里! Why is it happening? 为什么会这样?

Also, can you give me some tips on how to solve such problems in the future? 另外,您能否给我一些如何在将来解决此类问题的技巧?

The PUSH function is actually incrementing the pointer rearPointer instead of the value it points to. PUSH函数实际上是递增指针rearPointer而不是它指向的值。

Change 更改

*rearPointer++;

to

(*rearPointer)++;

To resolve this and other similar issue, you can run the app under debugger, I think the debugger will catch the exception and you can get some clue about what's wrong. 要解决此问题和其他类似问题,您可以在调试器下运行应用程序,我认为调试器将捕获异常,您可以获得有关错误的一些线索。

If you are using IDE, you can find the menu item with similar text as "run with debugger", or you can first run the app, and attach the debugger. 如果您使用的是IDE,则可以找到类似“使用调试器运行”的文本的菜单项,或者您可以先运行该应用程序,然后附加调试器。 You can add sleep for delay to make sure you have time to attach debugger. 您可以添加睡眠以延迟,以确保您有时间附加调试器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C ++程序在cin之后突然结束 - C++ Program abruptly ends after cin 程序甚至在调试器中突然结束 - 这是怎么发生的? - Program ends abruptly even in debugger - how did that happen? 该计划直接结束,为什么? - The program ends straight away, why? 为什么静态成员函数只能在类定义中声明为static而不是在自己的定义中? - Why can a static member function only be declared static inside the class definition and not also in its own definition? 为什么程序执行在for循环的第一次迭代结束? - Why the program execution ends at the first iteration of for loop? 为什么程序在运行之前就结束了? - Why does the program ends even before running? 有没有办法检查外部数据是否通过管道传输到程序或程序是否独立运行? - is there a way to check if external data is piped to a program or if program runs on its own? 为什么我的简单C ++程序与队列和fstream一起使用会出错,变成无效的指针? - Why is my simple C++ program working with queue and fstream erroring out into invalid pointer? 为什么队列接受vector作为其底层容器? - Why queue accepts vector as its underlying container? Qt程序挂起(无响应)直到功能结束然后再次开始工作 - Qt Program Hangs (Not Responding) Until Function ends then starts working again
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM