stack usage

This is a discussion on stack usage within the C++ Programming forums, part of the General Programming Boards category; I've got this code but I can't for the life of me figure out why I have an unhandled exception: ...

  1. #1
    Advanced Novice linucksrox's Avatar
    Join Date
    Apr 2004
    Location
    Michigan
    Posts
    198

    stack usage

    I've got this code but I can't for the life of me figure out why I have an unhandled exception:
    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    #include <string>
    #include <stack>
    
    
    class Indent
    {
    public:
        void processLine(string &str, int lineNum);
        Indent()
        {
            // Handy to push an initial entry on the stack
            // starting at location 0
            m_startingColStack.push(0);
        }
    private:
        stack<int> m_startingColStack;
    };
    
    void Indent::processLine(string &str, int lineNum)
    {
        int len = str.size();
        int startingCol = 0;
    
        // Find the location of the first non-blank
        // Assuming no TABs, .... TABs would take a little more work here.
        while (str[startingCol] == ' ' && startingCol < len)
            startingCol++;
        
        // Skip completely blank lines
        if (startingCol == len || len == 0)
            return;
        
        // If the new starting line is > top of stack, then push it on stack
        if (startingCol > m_startingColStack.top())
            m_startingColStack.push(startingCol);
        
        // Pop off the stack all of the starting locations > than
        //    the current starting position
        if (m_startingColStack.top() == startingCol && m_startingColStack.size() > 1)
            m_startingColStack.pop();
    
        // If the top of the remaining stack == the current starting position,
        //    we are in good shape, otherwise we have an error to report
        if (m_startingColStack.top() != startingCol)
            throw "Line is not properly indented!\n";
    }
    
    int main()
    {
        Indent indent;
    
    	ifstream infile("D://test.txt");
        string str;
        if (!infile)
        {
            cout << "Unable to open input file" << endl;
            cin.get();
            return EXIT_FAILURE;
        }
    
        try
        {
            string str;
            int lineNum=1;
            while (infile)
            {
                getline(infile, str);
                if (infile)
                {
                    indent.processLine(str, lineNum);
                }
                lineNum += 1;
            }
            cout << "Everything is properly indented " << endl;
        }
        catch (string e)
        {
            cout << e << endl;
        }
    
        cout << "\n\nPress enter to quit...\n\n";
        cin.get();
        return 0;
    }
    any ideas would be greatly appreciated.
    "What are all you parallelograms doing here?" - Peter Griffin (to Joe and his wheelchair buddies)

  2. #2
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Is it because you're using namespace std before including stack and string?
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  3. #3
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    You are throwing a char *.
    That's why you don't catch a string in main.
    works this way
    Code:
        if (m_startingColStack.top() != startingCol) {        
            throw string("Line is not properly indented!\n");
        }
    Kurt

  4. #4
    Advanced Novice linucksrox's Avatar
    Join Date
    Apr 2004
    Location
    Michigan
    Posts
    198
    thanks, that helps a lot! that would have taken a while to find on my own
    "What are all you parallelograms doing here?" - Peter Griffin (to Joe and his wheelchair buddies)

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. infix evaluation using stack
    By lewissi in forum C++ Programming
    Replies: 0
    Last Post: 11-03-2005, 02:56 AM
  3. Question about a stack using array of pointers
    By Ricochet in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2003, 10:12 PM
  4. error trying to compile stack program
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2003, 06:27 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