Design and develop a program for balanced delimiters checking . Use the stack class defined in STL. Your program should take from the user the name of the C++ program he wants to check and then opens it and processes its text and issues a message saying that:
You the program should check for the following delimiters:
1. [ and ]
2. ( and )
3. { and }
4. /* and */
5. “ and “

I am implementing a stack to check if a text file is well-formed (Every opening symbol has a closing one). It works fine for the first file but when I take a second file to read it does strange things.

while(count)
  {
	  stack.MakeEmpty();
	  cout << "Please enter the file name:" << endl;
	  cin >> file;
	  inFile.clear();
	  inFile.open( file );
	  inFile.clear();
	  while(!inFile)
	  {
		  cerr << "Error! No such file or directory. Please reenter the file name: " << endl;
		  cin >> file;
		  inFile.clear();
		  inFile.open( file );
	  }
      while (balanced && inFile>>symbol)
      {
		  
		  if (symbol == '/')
		  {
			  inFile>>symbol;
			  if(symbol=='*')
			  {
				  inFile>>symbol;
				  while(symbol!='*' && !inFile.eof())
					  inFile>>symbol;
				  if(inFile.eof())
					  balanced=false;
				  else
				  { 
					  inFile>>symbol;
					  if(symbol=='/')
						  balanced=true;
				  }
		  
			  }
		  }
		  else if (IsOpen(symbol))
		  {
			  stack.Push(symbol);
		  }
		  else if (IsClosed(symbol))
		  {
			  if (stack.IsEmpty() )
			  {
				  balanced = false;
			  }
			  else
			  {
				  openSymbol = stack.Top();
				  stack.Pop();
				  balanced = Matches(symbol, openSymbol);
			  }
		  }
  
	  }
	  if (!stack.IsEmpty() || !inFile.eof()) balanced=false;

	  if (balanced)
		  cout << "YES" << endl;
	  else
		  cout << "NO" << endl;
	  cout << "Would you like to do another file check? (Y/N):" << endl;
	  cin  >> answer;
	  if( answer != 'Y' && answer != 'y' && answer != 'N' && answer != 'n'  )
	  {
		  cout << "Enter Y or N !" << endl;
		  cin  >> answer;
	  }
	  if(answer == 'Y' || answer == 'y'){
		  count=true;
		  stack.MakeEmpty();
	  }
	  else if(answer == 'N' || answer== 'n')
		  count=false;
  }

but please I want to know how to write it with templates
Here is StackType.h:

#pragma once
#define MAX_ITEMS 20
/*class FullStack
// Exception class used by Push when stack is full.
{} ;

class EmptyStack
// Exception class used by Pop and Top when stack is empty.
{};*/
class StackType
{
public:
   StackType();
   ~StackType();
   bool IsEmpty() const;
   bool IsFull() const;
   void MakeEmpty();
   void Push(char item);
   void Pop();
   char Top() const;
private:
   int top;
   char items[MAX_ITEMS];
};

StackType.cpp:

#include "StackType.h"
#include <iostream>
#include <fstream>
#define MAX_ITEMS 20
#include <stdlib.h>
#include <string>
using namespace std;
StackType::StackType()//Constructor
{
  top = -1;
}


StackType::~StackType()//Destructor
{
}
bool StackType::IsEmpty() const//returns true if the stack is empty
{
  return (top == -1);
}
bool StackType::IsFull() const//returns true if the stack is full
{
  return (top == MAX_ITEMS-1) ;
}
void StackType::Push(char newItem)
{
  if (IsFull())
  {
    cout << "FullStack!";
	exit(1);
  }
  top++;
  items[top] = newItem;
}

void StackType::Pop()
{
	if(IsEmpty())
	{
		cout << " EmptyStack!";
		exit(1);
	}
  top-- ;
}
void StackType::MakeEmpty()
{
	top = -1;
}
char StackType::Top() const
{
	if (IsEmpty())
	{
		cout << "EmptyStack!";
		exit(1);
	}
  return items[top];
}


bool IsOpen  (char symbol);
bool IsClosed(char symbol);
int  Matches (char symbol, char openSymbol);

int main()
{
  char symbol;
  char answer;
  bool count=true;
  char file[MAX_ITEMS];
  StackType stack;
  bool balanced = true;
  char openSymbol;
  ifstream inFile;
  while(count)
  {
	  stack.MakeEmpty();
	  cout << "Please enter the file name:" << endl;
	  cin >> file;
	  inFile.clear();
	  inFile.open( file );
	  inFile.clear();
	  while(!inFile)
	  {
		  cerr << "Error! No such file or directory. Please reenter the file name: " << endl;
		  cin >> file;
		  inFile.clear();
		  inFile.open( file );
	  }
      while (balanced && inFile>>symbol)
      {
		  
		  if (symbol == '/')
		  {
			  inFile>>symbol;
			  if(symbol=='*')
			  {
				  inFile>>symbol;
				  while(symbol!='*' && !inFile.eof())
					  inFile>>symbol;
				  if(inFile.eof())
					  balanced=false;
				  else
				  { 
					  inFile>>symbol;
					  if(symbol=='/')
						  balanced=true;
				  }
		  
			  }
		  }
		  else if (IsOpen(symbol))
		  {
			  stack.Push(symbol);
		  }
		  else if (IsClosed(symbol))
		  {
			  if (stack.IsEmpty() )
			  {
				  balanced = false;
			  }
			  else
			  {
				  openSymbol = stack.Top();
				  stack.Pop();
				  balanced = Matches(symbol, openSymbol);
			  }
		  }
  
	  }
	  if (!stack.IsEmpty() || !inFile.eof()) balanced=false;

	  if (balanced)
		  cout << "YES" << endl;
	  else
		  cout << "NO" << endl;
	  cout << "Would you like to do another file check? (Y/N):" << endl;
	  cin  >> answer;
	  if( answer != 'Y' && answer != 'y' && answer != 'N' && answer != 'n'  )
	  {
		  cout << "Enter Y or N !" << endl;
		  cin  >> answer;
	  }
	  if(answer == 'Y' || answer == 'y'){
		  count=true;
		  stack.MakeEmpty();
	  }
	  else if(answer == 'N' || answer== 'n')
		  count=false;
  }
  cin.get();
  cin.get();
  inFile.close();
  return 0;
}
bool IsOpen(char symbol)
{
  if ((symbol  ==  '(')  ||  (symbol  ==  '{')  ||  (symbol  ==  '['))
    return true;
  else
    return false;
}

bool IsClosed(char symbol)
{
  if ((symbol  ==  ')')  ||  (symbol  ==  '}') || (symbol  ==  ']'))
    return true;
  else
    return false;
}

int  Matches(char symbol, char openSymbol)
{
  if(       ((openSymbol  ==  '(')  &&  symbol  ==  ')')
        ||  ((openSymbol  ==  '{')  &&  symbol  ==  '}')
        ||  ((openSymbol  ==  '[')  &&  symbol  ==  ']'))
		return 1;
  else  return 0;
}

My guess is you didn't initialize your values after finishing your first file. Pointers, counters, and flags are probably still the same values they were at the end of the file and not properly set back to their initial state for the next file.

This article has been dead for over six months. Start a new discussion instead.