0

Hi there ever1. I have written a bracket checker program using stack, seems to me its working ok, but still I have one problem with it.Well, it compiles ok,but when you are entering value, it just doesn't show you any results, but it supposed to give you the results.Any help in finding the problem will be appreciaed.Thanks.
Heres the code:

#include<iostream>

using namespace std;

typedef struct Node
{	char data;
	struct Node *next;
}node;

void push(char);
void display_stack();

node  *top;

int main()
{
 char check();
 char temp[20];
 int i=0,flag=1;

 system("CLS");
 cout << "\nPlease Enter Math Expression:\n" << endl;
 top=NULL;
 temp[i]= cin.get();
 
 if ( (temp[i]==')') || (temp[i]==']') || (temp[i]=='}') )
    { 
      cout << "\n Bad Expression! Please Run it Again... \n" << endl; 
      cin.get(); 
      return 1; 
    }
 else
  {
   while ( (int)temp[i]!=13 && (flag==1))
    {

     switch (toupper(temp[i]))
       {
        case '(': { push(temp[i]);  break; }
        case '[': { push(temp[i]);  break; }
        case '{': { push(temp[i]);  break; }
        case ')': { push(temp[i]);  break; }
        case ']': { push(temp[i]);  break; }
        case '}': { push(temp[i]);  break; }
       
        //case ')': { flag=check(temp[i]);}
        //case ']': { flag=check(temp[i]);}
        //case '}': { flag=check(temp[i]);}
       }
      i+=1;
      temp[i] = cin.get();
    }

  }
 if (flag==1) cout << "\nOK\n" << endl;
   else cout << "\nNO!\n" << endl;

 return 0;
};

void push(char y)
{
	node *ptr;
	ptr= new node;
	ptr->data = y;
	ptr->next = top;
	top = ptr;
}

void display_stack()
 {
 int i =0;
 node * temp;
 temp = top;
 while(temp!=NULL)
  {
  cout << "\nNode :  Value =    next " << endl;
  i++,temp->data,temp->next; 
  temp=temp->next;
  }
 }

/* REMOVES TOP NODE FROM  THE STACK AND RETURNS ITS VALUE*/

char pop()
{
  char a;
  if(top==NULL)
     { 
      cout << ("\n\t\tSTACK EMPTY...\n\n") << endl; return 0; 
     }
  else
    {
    a=top->data;
    cout << ("\n\n\n Pop: value to be returned : c ",a) << endl;
    free(top);
    top=top->next;
    return a;
    }
}

int check(char x)
{
   char tmp;
   tmp = pop();
   switch (tmp)
   {
    case ')': { if( tmp-x!=1 ) return 0; break;}
    case ']': { if( tmp-x!=2 ) return 0; break;}
    case '}': { if( tmp-x!=2 ) return 0; break;}
   }
   return 1;
}
2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by vmanes
0

On my system, this line

while ( (int)temp[i]!=13 && (flag==1))

keeps the program wanting data forever!
Why are you comparing to integer value 13? Where do you get that magical number?
Better to let the compiler figure out when you've reached the newline, in whatever encoding system is being used by your OS.

Try this

while ( temp[i] != '\n'  && ( flag == 1 ) )

Side note. In your display_stack function, what does the middle line in this loop do?

while(temp!=NULL)
   {
      cout << "\nNode :  Value =    next " << endl;
      i++,temp->data,temp->next; 
      temp=temp->next;
   }
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.