What is wrong with this:

for(int b = 0;b < c.length();b++)
      {
        temp[b] << S.Pop();  // or temp[b] = S.Pop();
      }

to have a string made up of the contents of the stack?

When I do this and cout << temp, temp is blank. This iterates through the loop properly, it just does not place the characters from the stack into the string.

Dani AI

Generated

A few quick, practical notes that clear up the usual causes when moving characters out of a stack into a string — following ’s report and ’s request for details.

Common root causes: using std::stack::pop() as if it returns a character (it does not — pop() is void), indexing a std::string without first sizing it (operator[] does not grow the string), forgetting the terminating '\0' for C-style arrays, or accidentally using the wrong operator (stream/bitshift instead of assignment). Also remember that popping a stack produces LIFO order, so the produced string may be reversed compared to the original input.

Two reliable patterns:

  • Use top() and then pop() and append the character to a std::string (or push_back) while the stack is not empty. If you need the original order, reverse the string after the loop.
  • If using a C-style buffer, set the buffer size in advance and write characters into it, then explicitly add a null terminator before printing.

Troubleshooting checklist:

  • Confirm temp is a std::string with sufficient size (or call temp.resize(n) or use temp.push_back()).
  • If using char[], ensure temp[n] = '\0'.
  • Verify whether your stack implementation’s pop() returns the popped value or is void; with std::stack use top() followed by pop() (see the reference).
  • Print sizes and individual character codes while debugging to spot '\0' or out-of-range writes.
    For reference: see the standard notes on std::stack::pop() and on std::string::resize() for correct use and behavior (std::stack::pop, std::string::resize).

Recommended Answers

All 2 Replies

please tell us more about your code

how have u declared temp....defination of pop() function

Sorry, I have managed to solve the issue.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.