1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Palindrome using stacksssss

Discussion in 'C++' started by spamfree4m3, May 13, 2008.

  1. spamfree4m3

    spamfree4m3 New Member

    Hey guys,

    I am trying to build a program that can read a palindrome from a stack that i create... Below is my code that i have come up with some far but it seems that i am missing a few things. The current error i get is that constructers are not allowed a return type for the stack :: stack()... Also in my main (which i dont have yet) i am guessing that that is where i grab the string from the user? the cin?

    Any help is welcomed!!

    Thanks in advance!!

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    
    class stack
    {
    public:
    	stack ();
    	stack(int n);
    	void push(char x);
    	void pop(char B);
    	bool isempty();
    	bool isfull();
    private:
    	int TOP;
    	char * storage;
    	int size;
    }
    
    
    stack :: stack()
    {
    	storage = new char[5];
    	TOP = -1;
    }
    
    bool stack :: isempty()
    {
    	return TOP == -1;
    }
    
    stack :: stack (int n)
    {
    	size = n;
    	storage = new char[n];
    	TOP = -1;
    }
    
    bool stack :: isfull()
    {
    	return TOP == size -1;
    }
    
    void stack :: push(char x)
    {
    	if (isfull())
    	
    		cout<<"Stack is full"<<endl;
    	
    	else
    		
    		TOP++;
    		storage[TOP]=x;
    	
    }
    
    void stack :: pop(char B)
    {
    	if (isempty())
    	{
    		cout<<"Stack is empty"<<endl;
    	}
    	else
    	{
    		B = storage[TOP--];
    	}
    }
    
    
     
  2. spamfree4m3

    spamfree4m3 New Member

    Not sure if anyone is even on these forums anymore... BUt i worked with the code some more and kind of chnaged everything around and now i have this..

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    
    
    class stack
    {
    public:
           stack ();
           stack (int n);
           void push (char x);
           char pop ();
           bool isempty ();
           bool isfull ();
    
    private:
           int TOP;
           char *storage;
           int size;
    };
    
    stack::stack()
    {
    	   storage = new char [28];
    	   TOP=-1;
    }
    
    stack::stack(int n)
    {
           size = n;
           storage = new char [size];
           TOP = -1;
    }
    
    bool stack::isempty() 
    {
           return TOP == -1;
    }
    
    bool stack::isfull() 
    {
           return TOP == size - 1;
    }
    
    void stack::push(char x) 
    {
           if (isfull())
                   cout << "I'm sorry, the stack is currently full." << endl;
           else
                   storage[++TOP] = x;
    }
    
    char stack::pop() 
    {
    	   char b;
    	if (isempty())
                   cout << "I'm sorry, the stack is currently empty." << endl;
           else
       
    		   b = storage [TOP--];
    			return b;
    }
    
    void main()
    {
    	string x;
    	int i = 0;
    	stack stackone;
    	stack stacktwo;
    	stack stackthree;
    	cout<<"Please enter a word or phrase:"<<endl;
    	getline(cin, x);
    
    	while (i< x.length()-1)
    	{
    		if(x[i]>='a'&&x[i]<='z'||(x[i]>='A'&&x[i]<='Z'))
    			stackone.push(x[i]);
    			stacktwo.push(x[i]);
    			i++;
    	}
    
    	
    	while (!stackone.isempty()&&stackone.pop()==stackthree.pop());
    	{
    		if(stackone.isempty())
    			cout<<"This is a palindrome."<<endl;
    		else
    			cout<<"This is not a palindrome."<<endl;
    	}
    
    
    
    }
    

    But everything just puts out, This is not a palindrome, and for some reason it posts the stack is empty like 4 times and i cant fiugre out why
     

Share This Page