C ++ while循环不会在错误条件下退出 [英] c++ while loop doesn't exit on false condition

查看:110
本文介绍了C ++ while循环不会在错误条件下退出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码有问题,导致运行时错误。为了调试代码,我在一些cout语句中添加了代码,以查找代码可以正确执行的最后位置。根据输出,当while条件评估为false时,while语句会中断,但是我看不到这是怎么可能的。代码如下:

I'm having a problem in my code that results in a run time error. To debug the code I've thrown in some cout statements to find the last location that code executes properly. Based on the output it looks like the while statement breaks when the while condition evaluates to false, but I can't see how that's possible. Here's the code:

var声明:

queue<string> newOrder;
stack< vector<char> > opStack;
char symbol;

发生了填充堆栈和队列的事情,然后到达此代码:

stuff happens that populates the stack and queue, then this code is reached:

while(opStack.empty()==false){
    if(opStack.top()[1] != 'L'){
        cout<<"is stack empty?:"<<opStack.empty()<<endl;
        symbol = opStack.top()[0];
        newOrder.push(symbol);
        opStack.pop();
        cout<<"popped stack;"<<endl;
        cout<<"is stack empty?:"<<opStack.empty()<<endl;
    }
    else{
        break;
    }
}
cout<<"made it out of while loop";

if(opStack.top()[1] == 'L'){    
   opStack.pop();
}
else{
   errorEncountered = true;
}

这是输出:

is stack empty?:0
popped stack;
is stack empty?:1
RUN FAILED (exit value 1, total time: 1s)

因此,根据输出,堆栈在循环结束时为空。这应该使while循环条件为假,但是程序在while循环退出之前失败。那怎么可能呢?

So, based on the output, the stack is empty at the end of the loop. This should makes the while loop condition false, but the program fails before the while loop exits. How can that be possible? Does it have something to do with the way that stacks work?

推荐答案

while循环确实在错误的条件下退出。我没有进行适当的调试,这使我只能专注于代码的错误部分。

The while loop DOES exit on the false condition. I wasn't debugging properly, and that made me focus on the wrong part of the code.

问题实际上是最后一个提示之后发生的事情。之后的语句 if(opStack.top()[1] =='L'){,试图从空堆栈中获取顶层项目并导致运行失败。

The problem is actually what happens AFTER the last cout. The statement after that, if(opStack.top()[1] == 'L'){, tries to get the top item from the empty stack and causes the run to fail.

但是这怎么可能呢,因为我们从来没有在输出中看到使它脱离while循环?这是因为在程序崩溃之前,最后一个cout从未刷新到输出。我在最后一个cout中添加了<< endl ,并且达到了目的。出现使它脱离while循环,并且我能够查明程序中的实际问题。 (通过将最后一个if-else语句放在 if(opStack.empty()== false){// second if-else set} 中来解决)

But how is that possible, since we never see "made it out of while loop" in the output? It's because the last cout isn't ever flushed to the output before the program crashes. I added <<endl to the last cout, and that did the trick. "made it out of while loop" was displayed and I was able to pinpoint the real problem in the program. (solved it by putting the last if-else statement inside if(opStack.empty()==false){//second if-else set})

感谢@interjay指出我的原始但格式不正确的问题并没有使cout泛红: c ++ while循环条件在stack.empty()中不能很好地发挥作用

Thanks to @interjay for pointing out that cout wasn't getting flushed in my original but poorly formatted question: c++ while loop condition isn't playing nice with stack.empty())

这篇关于C ++ while循环不会在错误条件下退出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆