Help

This is a discussion on Help within the C++ Programming forums, part of the General Programming Boards category; i have created a class of bracket checker by using stack,, Can anybody help me with the coding errors,, Code: ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    112

    Help

    i have created a class of bracket checker by using stack,,
    Can anybody help me with the coding errors,,

    Code:
    #include<iostream.h>
    #include<conio.h>
    
    const int MAX=15;
    
    class Stack
       {
       char array[MAX];
       char top;
    
       public:
    
       Stack()
       {
       top=0;
       }
    
       void push(char a)
       {
    	if (top<MAX)
    	{
    	array[top]=a;
    	top++;
    	}
       }
    
     char pop()
     {
     top--;
     return array[top];
     }
    
     void isEmpty(char top)
     {
    	if (top == 0)
    	{
    	cout<<"Stack is EMPTY";
    	}
     }
    
     };
    
     class BracketChecker
     {
     char name[MAX];
     Stack a;
    
     public:
    
     void input()
     {
     cin>>name;
     }
    
     char* getinput()
     {
     return name;
     }
    
     void function()
     {
    	 for (int j = 0; j <MAX; j++)
        {
        name[j];
         if (name =='(' || '{' || '[')
         {
         a.push(name);
         }
         if (name ==')' || '}' || ']' )
         {
    	if (!a.isEmpty())
    	{
    	a.pop();
    
    	if ((name == '}') || (name == ']')
    	      || (name == ')')
    
    	}
         }
    
         }
    };
    
    int main()
    {
    clrscr();
    BracketChecker b;
    b.input();
    b.function();
    getch();
    return 0;
    }
    Last edited by Fatima Rizwan; 11-25-2009 at 12:39 PM.

  2. #2
    Registered User
    Join Date
    Oct 2009
    Posts
    48
    Sure, at the top, use this instead
    Code:
    #include<iostream>
    #include<conio>
    Ok, please mark as SOLVED.

    Seriously though, what kind of errors are you getting?

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    112
    Void function is totally wrong ,, Can u help me with that??

  4. #4
    Registered User
    Join Date
    Oct 2009
    Posts
    48
    Code:
    if (name =='(' || '{' || '[')
         {
         a.push(name);
         }
         if (name ==')' || '}' || ']' )
    This part is wrong though I see you had the right idea after it. You need a condition that evaluates to true or false (or 1 or 0) in between || or &&. '{' does not evaluate to true of false so change that to (name == '{') and what is name? It is an array of chars, you should be using name[i] there.

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    112
    Code:
    
    void function()
    
     {
    	for (int j = 0; j <MAX; j++)
    	{
    
    		if (name[j] =='(' || '{' || '[')
    		{
    		 a.push(name[j]);
    		}
    
    			if (name[j] ==')' || '}' || ']' )
    			{
    			if (a.isEmpty() !==0)
    			{
    			a.pop();
    			if ((name[j] == '}') || (name[j] == ']') || (name[j] == ')') )
    			cout<<"valid"; }
    
    			else if (a.isEmpty()==0)
    			{
    			cout<<"Invalid:";
    			}
    	  }             }
    
    }


    Now still its not working!!
    Error: Functions containning for are not expanded inline!!

  6. #6
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    Quote Originally Posted by Fatima Rizwan View Post
    Code:
    
    void function()
    
     {
    	for (int j = 0; j <MAX; j++)
    	{
    
    		if (name[j] =='(' || '{' || '[')
    		{
    		 a.push(name[j]);
    		}
    
    			if (name[j] ==')' || '}' || ']' )
    			{
    			if (a.isEmpty() !==0)
    			{
    			a.pop();
    			if ((name[j] == '}') || (name[j] == ']') || (name[j] == ')') )
    			cout<<"valid"; }
    
    			else if (a.isEmpty()==0)
    			{
    			cout<<"Invalid:";
    			}
    	  }             }
    
    }


    Now still its not working!!
    Error: Functions containning for are not expanded inline!!
    Error

    [code]
    if (name[j] =='(' || '{' || '[')
    [code]

    should be

    Code:
    if (name[j] =='('  || name[j] == '{' || name[j] == '[')

  7. #7
    Registered User
    Join Date
    Mar 2009
    Posts
    112
    Code:
    if (a.isEmpty() !==0)
    invalid

  8. #8
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    Quote Originally Posted by Fatima Rizwan View Post
    Code:
    if (a.isEmpty() !==0)
    invalid
    See in your code

    [code]
    void isEmpty(char top)
    {
    if (top == 0)
    {
    cout<<"Stack is EMPTY";
    }
    }
    [code]

    the signature and def of function it didn't return anything but

    Code:
    if (a.isEmpty() !==0)
    here how can you compare with the function return value as it didn't meant to return any thing

  9. #9
    Registered User
    Join Date
    Mar 2009
    Posts
    112
    Code:
    #include<iostream.h>
    #include<conio.h>
    
    const int MAX=15;
    
    class Stack
       {
       char array[MAX];
       char top;
    
       public:
    
       Stack()
       {
       top=0;
       }
    
       void push(char a)
       {
    	if (top<MAX)
    	{
    	array[top]=a;
    	top++;
    	}
       }
    
     char pop()
     {
     top--;
     return array[top];
     }
    
     int isEmpty()
     {
     if ( top == 0)
     { cout<<"Empty stack"; }
     return 0;
     }
    
     };
    
     class BracketChecker
     {
     char name[MAX];
     Stack a;
    
     public:
    
     void input()
     {
     cin>>name;
     }
    
     char* getinput()
     {
     return name;
     }
    
     void function()
      {
    	for (int j = 0; j <MAX; j++)
    	{
    		if (name[j] =='(' || name[j]== '{' || name[j]=='[')
    		{
    		 a.push(name[j]);
    		}
    
    			if (name[j] ==')' || name[j]=='}' || name[j]==']' )
    			{
    			if (!a.isEmpty())
    			{
    			a.pop();
    			if ((name[j] == '}') || (name[j] == ']') || (name[j] == ')') )
    			cout<<"valid"; }
    
    			else if (a.isEmpty()==0)
    			{
    			cout<<"Invalid:";
    			}
    	  }             }
    
    }
    };
    
    int main()
    {
    clrscr();
    BracketChecker b;
    b.input();
    b.function();
    getch();
    return 0;
    }

  10. #10
    Registered User
    Join Date
    Mar 2009
    Posts
    112
    What ever sequence i give it says valid input,, I dun know how to pop and compare the brackets,, Can you help me with the highlighted part??

  11. #11
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    Quote Originally Posted by Fatima Rizwan View Post
    What ever sequence i give it says valid input,, I dun know how to pop and compare the brackets,, Can you help me with the highlighted part??
    Logic will be

    there will be 3 different braces types

    push and increment the brace type

    and pop the brace type

    if there is any value greater than 0 on the stack it means its invalid else vaild

  12. #12
    Registered User
    Join Date
    Mar 2009
    Posts
    112
    accordin to ur algorithm this is sequence would be true then )))((( althought its not,,!!

  13. #13
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    Quote Originally Posted by Fatima Rizwan View Post
    accordin to ur algorithm this is sequence would be true then )))((( althought its not,,!!
    put a constraint there that will check weather the ending braces are placed first or not.

  14. #14
    Registered User
    Join Date
    Mar 2009
    Posts
    112
    Code:
    #include<iostream.h>
    #include<conio.h>
    #include<string.h>
    const int MAX=15;
    
    class Stack
    {
    char array[MAX];
    int top;
    
    public:
    
    Stack()
    {
    top=0;
    }
    
    void push(char a)
    {
    if (top<MAX)
    {
    array[top]=a;
    top++;
    }
    }
    
    char pop()
    {
    top--;
    return array[top];
    }
    
    int isEmpty()
    {
    return top == 0;
    }
    
    };
    class BracketChecker
    {
    char name[MAX];
    Stack a;
      unsigned int m_stack_count;
      unsigned int m_angular_count;
      unsigned int m_square_count;
      unsigned int m_round_count;
    
    public:
    BracketChecker() : m_stack_count(0),
    	    m_angular_count(0),
    	    m_square_count(0),
    	    m_round_count(0) {}
    
    void input()
    {
    memset(name, 0, MAX);
    cin>>name;
    }
    
    char* getinput()
    {
    return name;
    }
    
    int function()
    {
    for (int j = 0; j < strlen(name); j++)
    {
    switch (name[j])
     {
          case '{':
    	a.push(name[j]);
    	++m_angular_count;
          break;
    
          case '[':
    	a.push(name[j]);
    	++m_square_count;
          break;
    
          case '(':
    	a.push(name[j]);
    	++m_round_count;
    
          break;
    
          case '}':
    	a.pop();
    	--m_angular_count;
          break;
    
          case ']':
    	a.pop();
    	--m_square_count;
          break;
    
          case ')':
    	a.pop();
    	--m_round_count;
    
          break;
    
    
       }
      }
      return status();
    }
    
    int status()
     {
      int result = 0;
      if (m_angular_count != 0 ||
          m_square_count  != 0 ||
          m_round_count   != 0)
        result = 1;
    
      return result;
    }
    };
    
    int main()
    {
    clrscr();
    BracketChecker b;
    b.input();
    b.function();
    int status = b.status();
    if (status == 1)
    cout<<"Valid\n";
    else
    cout<<"Invalid\n";
    getch();
    return 0;
    }

  15. #15
    Registered User
    Join Date
    Mar 2009
    Posts
    112
    Whatever input i give.. it says its valid,,!!

Page 1 of 3 123 LastLast
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