Thread: Stack Calculator Error

  1. #1
    Registered User
    Join Date
    Aug 2003

    Stack Calculator Error

    Hello, I am having a problem with the attached code.

    The problem is with the main loop; the calculator works once for any expression, but when it is looped back, there is a segmentation fault.

    I think it might have something do to with the input stream, but I am unable to fix it.

    Any help would be greatly appreciated!!!!


  2. #2
    Grammar Police HybridM's Avatar
    Join Date
    Jan 2003
    ok the first thing I would do is put in dummy cout statements within the function I think the error occurs, until I narrow it down to the problem statement.

    For example put a
    cout << "Entered read_and_evaluate." << endl;
    and the beginning of that function, and another reading "returning" or something at the end. You'll very quickly find the problem yourself.

    edit: I know it's frustrating having an error you can't find, but I think when you eventually discover and fix it, it's like a mini-high, for me at least for me.
    Thor's self help tip:
    Maybe a neighbor is tossing leaf clippings on your lawn, looking at your woman, or harboring desires regarding your longboat. You enslave his children, set his house on fire. He shall not bother you again.

    OS: Windows XP
    Compiler: MSVC

  3. #3
    Registered User
    Join Date
    Aug 2003
    I already know when the error occurs - the error is when it tries to pop something from the stack when there is nothing there.

    And this happens because when it tests the while loop in the read and evaluate function, it returns false the second time around.

    Anyone have any idea on how I can fix this?


  4. #4
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    You have twos problems. The first is that when you read choice in main() it leaves a newline in the input buffer so when your while loop tests if peek() returns '\n' it is, so the loop is not executed. The second part of the problem is that you never clear the newline after reading in the equation so that helps foul up the read of choice.

    Try (cin >> choice).get();
    and an ins.get(); at the end of your function.

  5. #5
    Registered User
    Join Date
    Aug 2003
    Wow, thank you so much.

    I never would have figured that one out.

    Thanks man


  6. #6

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stack and pointer problem
    By ramaadhitia in forum C Programming
    Replies: 2
    Last Post: 09-11-2006, 11:41 PM
  2. Question about a stack using array of pointers
    By Ricochet in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2003, 10:12 PM
  3. error trying to compile stack program
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2003, 06:27 PM
  4. What am I doing wrong, stack?
    By TeenyTig in forum C Programming
    Replies: 2
    Last Post: 05-27-2002, 02:12 PM
  5. Stack Program Here
    By Troll_King in forum C Programming
    Replies: 7
    Last Post: 10-15-2001, 05:36 PM