Stack Calculator Error

This is a discussion on Stack Calculator Error within the C++ Programming forums, part of the General Programming Boards category; Hello, I am having a problem with the attached code. The problem is with the main loop; the calculator works ...

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    14

    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!!!!

    -Thanks!

    http://www.onedollaranissue.com/code/stackcalc.cxx
    Attached Files Attached Files

  2. #2
    Grammar Police HybridM's Avatar
    Join Date
    Jan 2003
    Posts
    355
    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
    Code:
    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
    Posts
    14
    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?

    Thanks!

  4. #4
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    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
    Posts
    14
    Wow, thank you so much.

    I never would have figured that one out.

    Thanks man

    -Shane

  6. #6
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    Glad I can help

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-12-2006, 12:41 AM
  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, 03:12 PM
  5. Stack Program Here
    By Troll_King in forum C Programming
    Replies: 7
    Last Post: 10-15-2001, 06:36 PM

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