hi all... i m working on a C++ prgm with following definition:
Program Definition: Write a program to perform the following on a source file of any c++ program(File shud be included):
1. Count the line of code (LOC) of a source code file
2. Count the total number of statements. (i.e. Semicolons)
3. Count the total number of Logical Statements. (i.e. Closing Braces)
4. Count selected Key words(if,else,do,while etc).
5. Blank lines and comments should be removed from the LOC.
Now i m able to do the first 3 conditions, but the remaining 2 options I m not able to do... so pls anyone of u know how to work on files with string comparision then pls help me in this.
I have written the following code:

#include<iostream.h>
#include<fstream.h>
#include<conio.h>

void main()
{
	clrscr();
	int cnt=0,cnt1=0,cnt2=0;
	ifstream OpenFile("prog21.cpp");
	char ch;

	while(!OpenFile.eof())
	{
		OpenFile.get(ch);
		if(ch==10)
		cnt++;
		if(ch==59)
		cnt1++;
		if(ch==125)
		cnt2++;

	}

	cout<<"\nTotal Lines of code is  : "<<cnt+1;
	cout<<"\nTotal Statements are    : "<<cnt1;
	cout<<"\nTotal Closing Braces are: "<<cnt2;

	OpenFile.close();
	getch();
}

Edited 6 Years Ago by Nick Evan: Added code-tags

Read the file by line, not by character because you will need to parse the words in the file in order to find key words. So read the file using getline() instead of get().

To do #4 you will have to make an array of key c++ words. The easiest way to do that is probably with google. I have seen several sites that list c++ keywords, so you can just copy/paste it into your program then make an array of strings out of them.

Next, check each line read with getline() to see if it contains any of the keywords. If it does, then increment the keyword counter. But make sure it really is a keyword and not a variable name that just happens to have a keyword embedded in the name.

hey dragon.. thanks for replying but i m still not able to understand how to use getline() function to read a file. Also i m not able to use the string class.. Undefined symbol "string" is the error i m getting, i have included string header file both with and w/o .h extension. I m using TC3 compiler. so pls help me with this. if possible show me a small program for getline function to read from a file. thanks again.

Edited 6 Years Ago by vishalkbhatt: n/a

what compiler are you using? iostream.h and fstream.h are obsolete. c++ std::string is declared in <string>

#include <string>
#include <iostream>
#include <fstream>
using namespace std;

If your compiler can not compile the above then you are using an obsolete compiler, most likely Turbo C++. I don't know if that compiler even supports std::string class. If not, then you could use character array instead of std::string.

Here is a simple code snippet how to use getline() with character arrays.

ifstream in("filename.txt");
char buf[255];
while( in.getline(buf, sizeof(buf) ) ) // read until end of file
{

}

Thanks man, i got it working the getline() function. check the following code:

#include <iostream.h>
#include <fstream.h>
#include <conio.h>

int main()
{
	clrscr();
	ifstream openfile("file.cpp");
	char ch[255];
	while(!openfile.eof())
	{
	openfile.getline(ch,sizeof(ch));
	cout<<"\n"<<ch;
	}
	openfile.close();
	getch();
	return 0;
}

And ya i m using Turbo C ++ compiler which is not supporting String class. So now wat do i have to do for the 4 and 5 conditions of the original program definition?? or which compiler do u suggest who supports all the classes and functions?? Thanks again man.. I really appriciate ur help.

Edited 6 Years Ago by peter_budo: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)

If you have the option to use any compiler that you want then download and install either Code::Blocks with MinGW compiler, or Microsoft VC++ 2008 Express.

I already explained what you need to do for item #4.

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