Infix to postfix stack class problem

This is a discussion on Infix to postfix stack class problem within the C++ Programming forums, part of the General Programming Boards category; I am implementing a stack class to convert infix expressions to postfix expressions. Program runs when I enter : (A*(B+C)) ...

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    28

    Question Infix to postfix stack class problem

    I am implementing a stack class to convert infix expressions to postfix expressions.
    Program runs when I enter : (A*(B+C))
    for example and gives me the correct ABC+*
    But when I randomly entered (5+3)*3, I got a seg fault.

    I suspect it's something in the actual algorithm.
    Any ideas?
    Here is the relevant code.

    Edit: Code removed

    Any ideas or pointers would be appreciated
    Last edited by stefanyco; 10-05-2011 at 03:17 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,630
    Implement your algorithm using std::stack so that you can be more certain that you did not mess up the stack implementation.

    Oh, and this function really should not be part of your stack class template.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2011
    Posts
    28
    I agree, but pulling this out of main got it working somehow. And i am not confident with std::stack and how to implement using that so for now i just need to figure out why * after ) causes seg fault. Would it most likely be stack issues? Or my algorithm?

  4. #4
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Posts
    2,597
    Quote Originally Posted by stefanyco View Post
    I agree, but pulling this out of main got it working somehow. And i am not confident with std::stack and how to implement using that so for now i just need to figure out why * after ) causes seg fault. Would it most likely be stack issues? Or my algorithm?
    Test the stack separately...(but substituting it for std::stack or std::vector for a while would be better)
    My guess would be... that your implementation does not handle problems carefully. And due to some mistake in implementing the algorithm, you do something problematic.
    Last edited by manasij7479; 10-05-2011 at 01:01 AM.
    Manasij Mukherjee | gcc-4.9.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,630
    Quote Originally Posted by stefanyco
    And i am not confident with std::stack and how to implement using that
    Quote Originally Posted by stefanyco
    Would it most likely be stack issues? Or my algorithm?
    That's ironic though. At the moment you have two suspects: is your implementation of the conversion algorithm at fault, or is your stack implementation to blame? If you use std::stack, you only have one suspect. Certainly, it could be the case that you used the stack incorrectly, but at least it is still the same suspect code.

    Besides, conceptually the use of a stack is trivial: you just need to know the operations to push, to pop, to observe the top element, and to check if the stack is empty.

    Quote Originally Posted by stefanyco
    i just need to figure out why * after ) causes seg fault.
    Use a debugger. Place breakpoints and step through until you reach the seg fault. Then, you know that the code somewhere before that breakpoint has a bug.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Aug 2011
    Posts
    28
    Ah, I'll use a debugger. I somehow only thought debuggers were for assembly code. Haha.
    And i have implemented a normal basic stack this way successfully before, that's why i suspected its the algorithm. But youre right. Ill just assume its both and test everything.
    Thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Infix to postfix problem..
    By gaurav_13191 in forum C Programming
    Replies: 7
    Last Post: 12-18-2010, 01:04 PM
  2. Replies: 0
    Last Post: 05-12-2010, 01:30 AM
  3. Infix, Postfix, Pseudo-Calculator using Stack ADT
    By sangken in forum C Programming
    Replies: 9
    Last Post: 09-08-2006, 08:17 AM
  4. Replies: 4
    Last Post: 03-12-2006, 01:17 PM
  5. Infix to Postfix Coding Problem
    By megablue in forum C Programming
    Replies: 4
    Last Post: 07-29-2004, 05:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21