Closed Thread
Results 1 to 2 of 2

Thread: stack problem: print after pop function

  1. #1
    george_ya is offline Newbie
    Join Date
    Oct 2008
    Posts
    2
    Rep Power
    0

    stack problem: print after pop function

    have a problem with my program. It should be program that recognize palindome through the stack. Everything works great, only thing that don't work is printing stacks(original and reversed) after the funcion is done. Here is my entire code, and the problem is at case d and e:
    Code:
    #include <iostream>
    
    using namespace std;
    
    
    const int MAXSTACK = 21;
    class stack {
    private:
        int  stop;  
        char stk[MAXSTACK];
    public:
        stack();
        ~stack();
        stack(const stack& s);
        void push(const char c);
        char pop();
        char top(void);
        int  emptystack(void);
        int  fullstack(void);
        void stack_print(void);
        int stack::create(void);
    };
    stack::stack()
    {
        stop = 0;
    }
    stack::~stack() { }  
    stack::stack(const stack& s)
    {
        stop = s.stop;
        strcpy(stk,s.stk);
    }
    void stack::push(const char c)
    {
        stk[stop++] = c;
    }
    char stack::pop()
    {
        return stop--;
    }
    char stack::top(void)
    {
        return stk[stop - 1];
    }
    int  stack::emptystack(void)
    {
        return !stop; 
    }
    int  stack::fullstack(void)
    {
        return stop == MAXSTACK;
    }
    void stack::stack_print(void)
    {
        for (int i=0; i<stop; i++)
            cout<<stk[i];
        cout<<endl;
    }
    int  stack::create(void)
    {
        return !stop; 
    }
    char menu()
    {
    
        char volba;
    
        cout<<"\n";
        cout<<" **********.\n";
        cout<<"\n";
        cout<<" a ... make new containers\n";
        cout<<" b ... delete content\n";
        cout<<" c ... enter string\n";
        cout<<" d ... print on screen first stack\n";
        cout<<" e ...  print on screen first stack\n";
        cout<<" f ... is it palindrom\n";
        cout<<" x ... exit\n";
        cout<<"\n your choice : ";
    
        cin >>  volba;
        return volba;
    }
    int main() {
        char  palindrome[MAXSTACK]; 
        char volba;
        stack original,reversed;
        int   stackitems = 0,i;
        //cin.getline(palindrome,MAXSTACK);
        do{
            volba = menu();
            switch (volba)
            {
            case'a':
                {
                    original.create();
                    reversed.create();
                    cout<<"done'";
                    break;
                }
            case'b':
                {
                original.emptystack();
                reversed.emptystack();
                cout<<"empty";
                break;
                }
            case'c':
                {
                    cout<<"enter your string"<<endl;
                cin.get();
                //cin.get();
                cin.getline(palindrome,MAXSTACK);
        for(int o = 0; o < strlen(palindrome); o++)
    
            if (isalpha(palindrome[o]))
            {
                original.push(tolower(palindrome[o]));
                stackitems++;                           
            }
                original.stack_print();
    
            break;
                }
            case'd':
                {
                    original.~stack();
                    for(int g = 0; g < strlen(palindrome); g++)
                    original.push(tolower(palindrome[g]));
                    original.stack_print();
                }
                /*//cin.getline(palindrome,MAXSTACK);
        for(int g = 0; g < strlen(palindrome); g++)
    
            if (isalpha(palindrome[g]))
            {
                original.push(tolower(palindrome[g]));
                stackitems++;                           
            }
    
                }
                original.stack_print();*/
                break;
    
    
            /*{
                    cout<<"original: ";
            original.stack_print();
                    break;
                }*/
                break;
            case'e':
                {
                cout<<"reversed:"<<endl;
                for( i = 0; i < stackitems; i++) {
                reversed.push(original.top());
                original.pop();
            }
            reversed.stack_print();
                }
                break;
    
            case'f':
                {
                for( i = 0; i < stackitems / 2; i++) {
                reversed.push(original.top());
                original.pop();
            }
    
    
            if (stackitems % 2)
                original.pop();
    
            while (!original.emptystack()) {
                if (original.top() != reversed.top()) break;
                original.pop(); reversed.pop();
            }
            if (original.emptystack())
                cout << "it is palindrom\n";
            else
                cout << "not palindrom\n";
    
            break;
    
                }
            default:cout<<"!??!";
    
    
            }
        } while(volba!='x');
    }
    this destutctor thing was just something i tried, but didn't work

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Korsicall is offline Newbie
    Join Date
    Apr 2009
    Posts
    15
    Rep Power
    0

    Re: stack problem: print after pop function

    Whats the error you get? If everything else works fine, from a brief glance it should work.

    One change of code that you can do:

    from
    Code:
    stack2.push(stack1.top())
    stack1.pop()

    to this
    Code:
    stack2.push(stack1.pop())
    It doesn't do anything different, but I always find it easier to look at.

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 21
    Last Post: 06-11-2011, 07:46 PM
  2. General Stack problem in bootloader (it's written by myself)
    By kuraga in forum Assembly
    Replies: 9
    Last Post: 12-30-2010, 02:08 AM
  3. Exported DLL Function leaving stuff on stack
    By Ewe Loon in forum C and C++
    Replies: 1
    Last Post: 02-18-2010, 05:11 PM
  4. Replies: 2
    Last Post: 11-18-2009, 09:13 AM
  5. Replies: 6
    Last Post: 10-12-2009, 08:56 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts