Thread: I can't figure out why

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

    I can't figure out why

    The only problem Im having is that my program always displays "Unmatched grouping symbols". I know its in my main program in my while loop. I just cant figure out what's causing it. I've tried to use the Debugger but I dont understand what too look for.

    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);
    	}
    else
    	cin.ignore(s);
    
    if(Closer(s)){
    	S.pop(s);
    }
    
    while(S.pop(s)){
    	if(Match(Opener(s), Closer(s)))
    		cout << "Grouping symbols used properly";
    	
    	else 
    		cout << "Unmatched grouping symbols";
    }
    
    }

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Match() takes 2 chars, but you are passing in 2 bools instead.
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Location
    Indiana
    Posts
    99
    Oh I think I know what u mean
    Code:
    while(S.pop(s)){
    	if(Match('(', ')') || ('[', ']') || ('{', '}'))
    		cout << "Grouping symbols used properly";
    	
    	else 
    		cout << "Unmatched grouping symbols";
    }
    The problem with this tho is that it always displays "Grouping symbols used properly". So maybe I need &&'s instead of ||'s?

  4. #4
    Registered User
    Join Date
    Feb 2009
    Location
    Indiana
    Posts
    99
    I changed it to this but Im still having the same problem. Is it because Im missing a part that somehow suppose to compare how many openers and closers I have. Which means if I have at least 1 or more than the other than I will display "Unmatched grouping symbols" ?

    Code:
    while(S.pop(s)){
    	if(Match('(',')') || Match('[', ']') || Match('{', '}'))
    		cout << "Grouping symbols used properly";
    	
    	else 
    		cout << "Unmatched grouping symbols";
    }

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You pop a character.
    If it's a (, then you need to match it with a )
    It's no good trying to match it with a ]
    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.

  6. #6
    Registered User
    Join Date
    Feb 2009
    Location
    Indiana
    Posts
    99
    Oooh wait let me see...so instead of saying S.pop(s)...should it be S.pop(')') then check it in to see if its a match? When u say You pop a CHARACTER...u mean what Im actually poppin off the stack right?

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Yes, pop() returns (via the reference parameter), the character at the top of the stack.

    So if you're looking at a ) in your string, you should be expecting to pop a (
    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.

  8. #8
    Registered User
    Join Date
    Feb 2009
    Location
    Indiana
    Posts
    99
    Ok I think I see what u mean but isnt a rule for the Stack first one in is the last one out? So wouldnt I always pop a Closer? I tried this:

    Code:
    S.pop('(');
    But I get an error: initial value of reference to non-const must be an lvalue. An lvalue means you can use the object on the left of an assignment operator correct? If thats true then what would I assign the bracket to?

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You pop into a variable, then compare two things...
    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.

  10. #10
    Registered User
    Join Date
    Feb 2009
    Location
    Indiana
    Posts
    99
    I got it finally. Here my final code if anybody wants to check it out:
    Code:
    #include <iostream>
    #include <string>
    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;
    	string s;
    	unsigned int i;
    	char a;
    
    	cout <<"Enter data:\n";
    	getline(cin, s);
    
    	for(i = 0; i < s.length(); i++){
    		if(Opener(s.at(i)))
    			S.push(s.at(i));
    
    		else if(Closer(s.at(i))){
    			if(S.IsEmpty()){
    				cout << "Found a Closing bracket before an Opening bracket or\n There are no Opening brackets or\n There's an extra Closing bracket";
    				cout << "\nGrouping symbols not used properly";
    				return 0;
    			}
    			else 
    				S.pop(a);
    				if(!Match(a, s.at(i))){
    					cout << "No matching bracket\n";
    					cout << "Grouping symbols not used properly";
    					return 0;
    				}		
    		}
    	}
    	if(!S.IsEmpty()){
    		cout << "There's still an Opening bracket\n";
    		cout << "Grouping symbols not used properly";
    	}
    	else
    		cout << "Grouping symbols were used properly";
    	return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. symmetryc problem
    By raizard in forum C Programming
    Replies: 13
    Last Post: 08-29-2010, 09:11 PM
  2. * Character in
    By Jonnyb42 in forum C Programming
    Replies: 14
    Last Post: 01-15-2010, 10:28 AM
  3. figure - problem
    By jamesfisher in forum C++ Programming
    Replies: 6
    Last Post: 11-10-2009, 05:28 PM
  4. 3 dimensional figure volume
    By thekautz in forum C++ Programming
    Replies: 2
    Last Post: 01-20-2009, 05:22 PM
  5. trying to figure out someone's code for a plugin
    By paulpars in forum C++ Programming
    Replies: 4
    Last Post: 07-20-2006, 10:57 AM