Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8773825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:24:12+00:00 2026-06-13T18:24:12+00:00

I’m having a problem in my code that results in a run time error.

  • 0

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 declarations:

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

this is the output:

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

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T18:24:13+00:00Added an answer on June 13, 2026 at 6:24 pm

    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.

    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.

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

    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())

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I have this code to decode numeric html entities to the UTF8 equivalent character.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.