Im stuck and can't figure this out

This is a discussion on Im stuck and can't figure this out within the C++ Programming forums, part of the General Programming Boards category; For this code Im using a class for a stack. I want to read a string then push any open ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Location
    Indiana
    Posts
    99

    Im stuck and can't figure this out

    For this code Im using a class for a stack. I want to read a string then push any open brackets on to the stack and pop the closing brackets off. If there are no brackets then I want it to ignore the string. Then I want it to display if they have been used properly. I test them with these:
    a+b
    (a-b)
    ((c-d)
    {e-f/[g+h]}
    (s+t]

    Code:
    #include <iostream>
    using namespace std;
    
    const int DefaultListSize = 50;
    typedef char Elem;
    
    class Astack 
    {
    private:
    	int top;	/*Index for top element*/
    	int size;	/*Maximum size of stack*/
    	Elem*listArray;	/*Array holding stack elements*/
    public:	
    		Astack(int sz =DefaultListSize)	/*Constructor*/
    		{size = sz; top = 0; listArray = new Elem[sz];}
    		~Astack() { delete [] listArray;}	/*Destructor*/
    		void clear() {top = 0;}
    		bool push(const Elem& item){
    			if(top == size) return false;	/* Stack is full*/
    			else {listArray [top++] = item;
    				return true;
    			}
    		}
    		bool pop(Elem& item){	/*Pop top element*/
    			if(top == 0) return false;
    			else {item = listArray[--top]; 
    				return true;
    			}
    		}
    		bool topValue(Elem& item) const {	/*Return top element*/
    			if (top == 0) return false;
    			else {item = listArray[top - 1];
    			return true;
    			}
    		}
    		int length() const {return top;}
    		bool IsEmpty() const {if(top == 0) return true;
    		else return false;
    		}
    	};
    
    	bool Opener(char ch){
    	 if((ch == '(') || (ch =='[') || (ch =='{'))
    		return true;
    	else 
    		return false;
    	}/*end of opener*/
    
    	bool Match(char Lc, char Rc){
    		if(( Lc == '(') && (Rc == ')') || ((Lc == '[') && (Rc == ']')) ||((Lc == '{') && (Rc == ']')))
    			return true;
    		else
    			return false;
    	}/*end of Match*/
    
    	bool Closer(char ch){
    		if((ch == ')') || (ch == ']') || (ch == '}'))
    			return true;
    		else
    			return false;
    	}/*end of closer*/
    
    int main(){
    	
    	Astack S;
    	char s;
    
    cout <<"Enter data:";
    cin >> s;
    
    if(Opener (s)){ 
    		S.push (s);
    }
    
    if(Closer (s)){
    		S.pop (s);
    }
    
    while(cin >> s){
    	if(Match){
    		cout << "Grouping symbols used properly";
    	}
    	else
    		cout << "Unmatched grouping symbols";
    }
    }
    It works it just doesn't work correctly. I know it's my main program that is wrong I just cant figure out what.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,500
    > if(Match)
    This doesn't call Match.

    It is just a bare function pointer, which always compares as not equal to NULL (therefore, always true).

    Nor are you popping things off your stack to compare with.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Location
    Indiana
    Posts
    99
    I dont see how Im not calling it. I thought it would be similiar to how I did Opener and Closer. Don't I want to say read my string, if my string is does a match then display grouping symbols used properly else they are not?

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    You are not passing any arguments to Match.

  5. #5
    Registered User
    Join Date
    Feb 2009
    Location
    Indiana
    Posts
    99
    ok, so I guess the question I need to ask then is what do I need to argue?

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Quote Originally Posted by jturner38 View Post
    ok, so I guess the question I need to ask then is what do I need to argue?
    You have declared the functions signature as such:

    Code:
    bool Match(char Lc, char Rc)
    So you need to pass in Lc and Rc when you call the function.

Popular pages Recent additions subscribe to a feed

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