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;
}

Dani AI

Generated

— the observed "no result" is almost always an input-loop problem plus a few logic/memory bugs. is right to flag the newline test: comparing against a magic integer for end-of-line will behave differently across systems and can make the loop never exit. Beyond that, the algorithm needs to push only opening brackets and, when seeing a closing bracket, pop and compare; the posted code pushes closers and leaves the matching code commented out, so no real checking happens.

Practical fixes and a minimal, robust approach:

  • Read the whole line with std::getline (portable and simple) instead of using cin.get() and testing a magic number.
  • Use std::stack<char> or, if you keep the linked list, make pop() safe: save top->next before deleting, use delete (not free), and return the popped char. Handle empty-stack on a closing bracket as an immediate mismatch.
  • Compare pairs explicitly (for example, '(' with ')' etc.) rather than relying on fragile ASCII arithmetic.

Example pattern to implement (use this instead of the old input loop and manual newline test):

#include <iostream>
#include <string>
#include <stack>

bool matches(char open, char close) {
  return (open=='(' && close==')') ||
         (open=='[' && close==']') ||
         (open=='{' && close=='}');
}

int main() {
  std::string line;
  if (!std::getline(std::cin, line)) return 0;
  std::stack<char> st;
  for (char ch : line) {
    if (ch=='('||ch=='['||ch=='{') st.push(ch);
    else if (ch==')'||ch==']'||ch=='}') {
      if (st.empty() || !matches(st.top(), ch)) { std::cout << "NO\n"; return 0; }
      st.pop();
    }
  }
  std::cout << (st.empty() ? "OK\n" : "NO\n");
}

Other notes: the display_stack loop line i++,temp->data,temp->next; does nothing — use cout << temp->data to print. Avoid system("CLS") in portable code. Add small test cases (balanced, unbalanced, extra closing, leftover openings) and step through with a debugger or printed traces to confirm behavior.

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;
   }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.