stack problem: print after pop function

This is a discussion on stack problem: print after pop function within the C++ Programming forums, part of the General Programming Boards category; have a problem with my program. It should be program that recognize palindome through the stack. Everything works great, only ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    3

    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. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
                    original.~stack();
    Surely this is quite wrong! You do not EVER [1] call the destructor in your own code. If you want to perform some task that the constructor performs, then make another member-function that does what you want to do, and let the destructor call this function.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM

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