Thread: plzzz help in a stack :(

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    17

    Unhappy plzzz help in a stack :(

    hi all

    i have a question and plz i really need your help
    the question is >>>>


    A string of characters has balanced parenthesis if each right parentheses occurring in the string is matched with a preceding left parentheses in the same way each right brace in a C++ program is matched with a preceding left brace. Write a program that uses a stack to determine whether a string entered at the keyboard has balanced parentheses.

    and this is what i did >>>>


    Code:
    #include <iostream>
    #include <string.h>
    using namespace std;
    
    #define SIZE 100
    char C1='(';
    char C2=')';
    
    class stack {
        private:
        char stackChar[SIZE];        // Holds the stack
        int top;
    
        public:
        stack()
        { top= 0; }
    
        void push(char ch)
        {
            if(top==SIZE)
            {
                cout << "Stack is FULL!!"<<endl;
            }
            stackChar[top] = ch;
            top++;
        }
    
        char pop(char ch)
        {
            if(top==0)
            {
            cout << "Stack is EMPTY!!" <<endl;
            return 0;
            }
            top--;
            return stackChar[top];
        }
    
        bool balanced (char *ch, char s)
        {
            int count=0;
            while(*ch!= '\0')
            {
                if(*ch==C1)
                {
                    push(C1);
                    count++;
                }
                if(*ch==C2)
                {
                    pop(C1);
                    count--;
                }
            }
            if (count==0)
            return true;
            else
            return false;
        }
    
    
    
    
    };
    
    int main()
    {
        stack St;
        char S1[SIZE]= "My name is (( lady bird ))";
        char S2[SIZE]= "(Im (23) years old ))";
        char *ch;
        cin.getline(S1,SIZE);
        if (St.balanced (ch, S1))
        cout<<"The first string is balanced "<<endl;
        else
        cout<<"The first string is unbalanced "<<endl;
    
        cin.getline(S2,SIZE);
        if (St.balanced (ch, S2))
        cout<<"The second string is balanced "<<endl;
        else
        cout<<"The second string is unbalanced "<<endl;
    
    
    
      return 0;
    }
    Last edited by ladybird__86; 11-13-2009 at 11:08 AM.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    So what do you need help with?

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    17
    Quote Originally Posted by Daved View Post
    So what do you need help with?

    in how can i read a string and push a character in a stack

    it appears arror in what i made in the main function !!!

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    What error do you get? A compile error? The code doesn't run correctly? If it doesn't give you the right answer, what input do you give? I don't see anywhere in the code where you ever call push to push something on the stack.

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Firstly, you really should practice const correctness. If something is not meant to be changed, then it should be const. If a function takes a parameter by reference or pointer, and the value referred to does not change, it should be marked as const.

    Second, your two calls to balanced do not match your prototype.

    Third, the second parameter of balanced() is unused.

    There may be more problems.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by Daved View Post
    What error do you get? A compile error? The code doesn't run correctly? If it doesn't give you the right answer, what input do you give? I don't see anywhere in the code where you ever call push to push something on the stack.
    It's in balanced.

    To the OP:
    It's a poorly designed class to have both balanced and conventional stack operations together with the same access level. Balanced should be a free function, cause checking the balance of parens is not part of the function of a stack, but rather an application. Alternatively rename stack to something like ParenBalancer, and make the push and pop operations private.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  7. #7
    Registered User
    Join Date
    Nov 2009
    Posts
    17
    my problem in how to read a string and push a character into a stack !!
    also the balanced function... i really dont understand how to make it

    and about the class i will try to correcr it....

  8. #8
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Your on the right track, just address the four points that I had. If you don't understand some of what I said, ask.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  9. #9
    Registered User
    Join Date
    Nov 2009
    Posts
    17
    Quote Originally Posted by King Mir View Post
    Your on the right track, just address the four points that I had. If you don't understand some of what I said, ask.
    ok i will

  10. #10
    Registered User
    Join Date
    Nov 2009
    Posts
    17
    Code:
    #include <iostream>
    #include <string.h>
    using namespace std;
    
    #define SIZE 100
    char C1='(';
    char C2=')';
    
    class stack {
        private:
        char stackChar[SIZE];        // Holds the stack
        int top;
    
        public:
        stack()
        { top= 0; }
    
        void push(char ch)
        {
            if(top==SIZE-1)
            {
                cout << "Stack is FULL!!"<<endl;
            }
            stackChar[top] = ch;
            top++;
        }
    
        void pop(char &ch)
        {
            if(top==0)
            {
            cout << "Stack is EMPTY!!" <<endl;
            }
            top--;
        }
    
        bool balanced (char &ch)
        {
            int count=0;
            while(ch!= '\0')
            {
                if(ch==C1)
                {
                    push(C1);
                    count++;
                }
                if(ch==C2)
                {
                    pop(C1);
                    count--;
                }
            }
            if (count==0)
            return true;
            else
            return false;
        }
    
        bool isEmpty ()
        {
            return top==-1;
        }
    
    
    };
    
    int main()
    {
        stack St;
        char S1[SIZE]= "My name is (( Zainab ))";
        char S2[SIZE]= "(Im (23) years old ))";
        char *ch;
    
            cin.getline(S1,SIZE);
            if (St.balanced (*ch))
            cout<<"The first string is balanced "<<endl;
            else
            cout<<"The first string is unbalanced "<<endl;
    
    
            cin.getline(S2,SIZE);
            if (St.balanced (*ch))
            cout<<"The second string is balanced "<<endl;
            else
            cout<<"The second string is unbalanced "<<endl;
    
    
    
      return 0;
    }

    i did some corrections... there is no errors but there is no outputs also

  11. #11
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Did you type in a string followed by enter? You need to because of your getline() call.

    As for error remaining:
    1) Use std::string instead of char arrays. They are easier to work with.
    2) If you use char arrays, you need to pass the array as a char pointer, not a char reference. You had that part right before.
    3) You need to advance though each character in balanced. This is why a pointer is needed.
    4) You're still not const correct. In addition to what I said before, you now have a method isEmpty() that you added. Since it does not change the class, it should be marked const too:
    Code:
    bool isEmpty () const
    5) stack is still a poorly designed class; balanced should be a free function.
    Last edited by King Mir; 11-13-2009 at 08:16 PM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  12. #12
    Registered User
    Join Date
    Nov 2009
    Posts
    17
    Quote Originally Posted by King Mir View Post
    Did you type in a string followed by enter? You need to because of your getline() call.

    As for error remaining:
    1) Use std::string instead of char arrays. They are easier to work with.
    2) If you use char arrays, you need to pass the array as a char pointer, not a char reference. You had that part right before.
    3) You need to advance though each character in balanced. This is why a pointer is needed.
    4) You're still not const correct. In addition to what I said before, you now have a method isEmpty() that you added. Since it does not change the class, it should be marked const too:
    Code:
    bool isEmpty () const
    5) stack is still a poorly designed class; balanced should be a free function.

    first of all thank you for helping me
    second:
    1) i didn't take std::string befor :/... so i have to keep it as char array
    2) i correct it
    3) you mean i need loop right !
    4) i correct it
    5) i dont understand :/ what do u mean by free function ! just the balanced function that i have to make it better? or there is another mistake in the other functions ?

  13. #13
    Registered User
    Join Date
    Oct 2009
    Posts
    48
    3) You're passing a pointer to a char array in balanced and your plan is to compare each char in the array until you reach '\0'. The problem is you're not advancing the pointer after the comparison.

    5) End the definition of the Stack class before defining and declaring balanced.This will make it free from the class. Then to call it, use 'balanced (string)' instead of using the dot. Look at what you're passing into balanced, though. You're passing a pointer to a char that hasn't been initialized. You want to pass it a string (char array technically). Why don't you comment out the getLine calls until you get balanced working and just pass it S1 and S2. In that case, S1 and S2 are const char *s so you need to change your balanced function to accept them. Your compiler may/may not complain if you don't do this.

    Good luck

  14. #14
    Registered User
    Join Date
    Nov 2009
    Posts
    17
    woooooooooooow lastly i did it

    thank you all of you for helping me

  15. #15
    Registered User
    Join Date
    Nov 2009
    Posts
    17
    i make the user who enter the strings it is much easier
    woooow

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stack and pointer problem
    By ramaadhitia in forum C Programming
    Replies: 2
    Last Post: 09-11-2006, 11:41 PM
  2. Question about a stack using array of pointers
    By Ricochet in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2003, 10:12 PM
  3. error trying to compile stack program
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2003, 06:27 PM
  4. What am I doing wrong, stack?
    By TeenyTig in forum C Programming
    Replies: 2
    Last Post: 05-27-2002, 02:12 PM
  5. Stack Program Here
    By Troll_King in forum C Programming
    Replies: 7
    Last Post: 10-15-2001, 05:36 PM