Problem writing my class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <cstring>

using namespace std;

class loc
{
	public:
		int counter();


};

int loc::counter()
{
	ifstream file;
	string stringline; 
	size_t found; 
	int positioncount = 0; 
	file.open("loc.cpp");
	int remove = 0;

	while(!file.eof())
	{
	getline(file, stringline);
	
	if(stringline.length()==0)
	{
		remove++;
	}
	else if(stringline.find("//") != std::string::npos)
	{
		remove++;
	}
	else if(stringline.find("/*") != std::string::npos)
	{
		remove++;
	}
	else if(stringline.find("*/") != std::string::npos)
	{
		remove++;
	}
	else positioncount++;	
	}

	cout << remove<< endl;
	cout << positioncount -1 << endl;
	file.close();
}

int main()
{

	counter();
	return 0;

}


So when I compile I get these errors:
class.cpp: In function ‘int main()’:
class.cpp:59: error: ‘counter’ was not declared in this scope

I also need to make it so that my program doesn't count block comments. I don't have any idea how to put that into code.

I had this program working without a class but I need to put it into one.
counter() is a non-static member function of class loc. Hence it can be accessed only through an object of that class.
1
2
loc locObject;
int val = locObject.counter();
That worked thanks!

Just one problem left and that's how i should go about making it so my counter doesn't count block comments?

These

1
2
3
4
/* I don't want
To count 
Any of these 
Lines */



So from that it shouldn't count those 4 lines.
Last edited on
I guess when you read a '/*' then you should continue removing lines till you get a '*/'.

Something like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
int loc::counter()
{
	ifstream file;
	string stringline; 
	size_t found; 
	int positioncount = 0; 
	file.open("loc.cpp");
	int remove = 0;

        bool inCommentSection = false;

        // change this because eof may happen on getline() and then you will be working on invalid stringline.
 	while( getline(file, stringline) )
	 {
	
	         if(stringline.length()==0)
	         {
 		       remove++;
	         }
 	        else if(stringline.find("//") != std::string::npos)
	         {
		       remove++;
 	        }
	         else if(stringline.find("/*") != std::string::npos)
	        {
		       remove++;
                       inCommentSection = true;
	        }
	        else if(stringline.find("*/") != std::string::npos)
	        {
		       remove++;
                       inCommentSection = false;
	        }
                else if (inCommentSection)
	        {
		       remove++;
	        }
	        else positioncount++;	
	}

	cout << remove<< endl;
	cout << positioncount -1 << endl;
	file.close();
}
Thanks again abhishekm71.

It all works but for some reason some lines from my code are not being counted. I run the program on itself and I'm always missing about 7 lines.

I've changed this:
1
2
3
cout << remove<< endl;
	cout << positioncount -1 << endl;
	file.close();


to this:

1
2
3
cout << remove<< endl;
	cout << positioncount  << endl;
	file.close();


That was leftover from when my code would give me 1 more line than there existed.

When I run the program on a small test file it correctly counts each line. Although I do notice that if I add comment lines to the end of some code it doesn't count that line of code.
Last edited on
The error is not in the code, but in the file that you are reading :)

The file loc.cpp already contains a "//", a "/*" and a "*/"
if(stringline.find("*/") != std::string::npos)
How could I make it so that it counts those executable lines?
Topic archived. No new replies allowed.