Hi all.. i have a program to count LOC i.e. Line of code of a source file of any C++ program. The LOC shud not contain the comments count and also the blank lines. Now i m able to count to total lines of the program but the comments and the blank line count is proving very tough for me. coz // and /* both type of comments shud not be counted. if // comes after some valid C++ statement then it shud be counted as an LOC. for exa.

int a ; // variable declared.

This shud be counted as an LOC

int b=10 ; /* variable declared
with the value of 10 /*

this shud be counted as one LOC and one comment line.
also how to count the blank lines ??
so pls anyone who can solve this problem then pls help me with this program.

Recommended Answers

All 2 Replies

Please post the relevant code that you have tried so far.

Looking at the problem statement that you have specified, you can see a pattern. (The semicolon in the statement.)

You can use that for identifying a LOC, right?

Using such patterns, you can accomplish your objective.

thanks for replying, but not only semicolon means a line termination.
also if else , loops are lines of code but not ending with semicolon.
Posting a code for counting // comments but not getting a right answer. pls help..

char ch2 , buf[2000];
ifstream openfile2("file.cpp");
	int i = 0;
	while(!openfile2.eof())
	{
	openfile2.get(ch2);
	buf[i]=ch2;
	i++;
	}

	for(int j = 0 ; j<sizeof(buf); j++)
	{
		if(buf[j]==10)
		{
			for(int k=j;k<sizeof(buf);k++)
			{
				if((buf[k+1]==32) || (buf[k+1]==9))
				{
					j++;
				}
			}
		}

		if((buf[j]==47) && (buf[j+1]==47))
		{
			cnt3++;
		}

	}
	cout<<"\n\tTotal Comments: " <<cnt3;
	//cout<<buf;
	openfile2.close();
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.