VB icon

Curly Bracket Counter

Email
Submitted on: 1/1/2015 6:41:00 AM
By: Jon Martin (from psc cd)  
Level: Beginner
User Rating: By 2 Users
Compatibility: C++ (general)
Views: 3223
 
     Counts all valid curly brackets in a c++ source file. Tells where a file's brackets may be out of balance OR tells the user if the brackets are in balance.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: Curly Bracket Counter
// Description:Counts all valid curly brackets in a c++ source file. Tells where a file's brackets may be out of balance OR tells the user if the brackets are in balance.
// By: Jon Martin (from psc cd)
//
// Inputs:file input
//
// Returns:output to screen
//
// Assumes:input file must be in same folder as .exe
(after compiled of course)
//
// Side Effects:none known
//**************************************

/*************************************************************************
J.R. Martin
Curly Bracket counter.
This program counts all valid curly brackets in c++
disregarding comment lines and paragraphs as well as constants (char and strings
including curly brackets).
*************************************************************************/
#include <iostream>
#include <cstdlib>
#include <string>
#include <fstream>
using namespace std;
void OpenInFile ( ifstream&, ofstream& );
void OpenOutFile ( ofstream& );
void CheckForBrackets ( ifstream&, ofstream& );
void GetComments ( char&, ifstream& );
void GetStringConstant ( char&, ifstream& );
void GetCharConstant ( char&, ifstream& ); 
int main()
{
	ofstream out;
	ifstream in;	
	
	
	OpenOutFile ( out );		
	OpenInFile ( in, out );			
	CheckForBrackets ( in, out );
									
	out << "\n\nThank you for choosing Jon's Software.\n\n";
return 0;
}						
void OpenInFile ( ifstream& SomeInFile, ofstream& out )
{
	string FileName; 
	cout << "This Program counts Curly Brackets for a C++ program file and\n" 
		<<	"validates if they are in balance. Please make sure you have placed your\n"
		<<	"file in the same folder as this program.\n\n";
		cout << "Please type the name of your input file: " << endl << endl;
	cin >> FileName;	
	cout << FileName;
	cout << endl << endl;
	
	SomeInFile.open(FileName.c_str());
	if ( !SomeInFile )					
	{
		out << "** Can't open " << FileName << " **" << endl;
		cout << "** Can't open " << FileName << " **" << endl;
		exit (1);
	}
}
void OpenOutFile ( ofstream& out )
{
	out.open( "output.dat" );			// Open the OutFile
		if ( !out )						// Test Outfile and warn if not opened 
		{
			out << "*** Can't open output file ***\n\n";
			cout << "*** Can't open output file ***\n\n";
			exit (1);
		}
}
void CheckForBrackets ( ifstream& in, ofstream& out )
{
	char character;
	int countLeft = 0;
	int countRight = 0;
	int totalCount = 0;
	int runningCount = 0;	
	in.get(character);
		while ( in && runningCount >= 0 )
		{
			while ( in && (character == ' ' || character == '\n') )
			{
				in.get(character);		
			}
			switch ( character )
			{
			case '/':	GetComments (character, in);		
															
				break;
			case '"':	GetStringConstant (character, in);	
														
				break;
			case '\'':	GetCharConstant (character, in);				
														
				break;
			}
		
			if ( character == '{' )
			{
				countLeft++;
				runningCount++;		
				totalCount++;
			}
			else if (character == '}' )
			{
				countRight++;
				runningCount--;		
				totalCount++;
			}			
			
			if ( runningCount < 0 )		
			{
				
				cout << "Your curly brackets are out of balance !!! " << endl << endl;				
				
				cout << "The Program has stopped AND the counters are at the following positions: " 
					<< endl << endl;
				cout << "Left's: " << countLeft << endl <<
					"Right's: " << countRight << endl << "Total count: "
					<< totalCount << endl << endl 
					<< "** USE THE ABOVE COUNTS TO FIND THE ERROR!! **" << endl << endl;
				exit (1);
			}			
			in.get(character);			
		}											
			
			cout << "The number of left brackets is " << countLeft << endl; //give totals
			cout << "The number of right brackets is " << countRight << endl;
			cout << "The total bracket count is " << totalCount << endl;			
			if ( runningCount == 0 )
			{
																				// validate counts
				cout << "The curly brackets {} are in balance." << endl;			// OK
			}
			if ( runningCount != 0 )
			{																
				cout << "The curly brackets {} are out of balance. " << endl;	// shun a bad count
																				// Not OK
			}
}
void GetComments ( char& c, ifstream& in )
{
	in.get(c);
	if ( c == '/' )						
	{
		while ( in && c != '\n' )		
		{
			in.get(c);
		}
	}	
	
	else if ( c == '*' )				
	{
		in.get(c);				
		while (in)
		{
			while ( in && c != '*' )	
			{
				in.get(c);
			}							
			if ( c == '*' )						
			{						
				in.get(c);
				if ( c == '/' )
					return;				
			}						
		}
	}
	else
		return;						
}
void GetStringConstant ( char& c, ifstream& in )
{
		do
		{
			in.get(c);
			while ( c == '\"' ) // get the escape sequence for \"
				in.get(c);
			if ( c == '\n' )
				return;
			
		}while ( in && c != '"' );
}
void GetCharConstant ( char& c, ifstream& in )
{				
		do
		{
			in.get(c);
			while ( c == '\'' ) 
				in.get(c);
			if ( c == '\n')		
				return;
			
		}while ( in && c != '\'' );
}
			


Other 1 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.