counter program

I have to make a program that counts lines of code and a separate counter that prints out each object name along with the total number of methods and object line of code.

This is what I have so far and it only counts my lines of code.

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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <cstring>
/*Testing
Block
Comment*/

using namespace std;

class loc
{
	public:
	int counter();
	int objectl();
	int methods();
	int methodcounter();


	private:
	
	//int objlines;
	int method;

};


/*
int loc::objectl()
{
	return objlines;
}
*/
int loc::methods()
{	
	return method;
}


int loc::counter()
{
	ifstream file;
	string line; 
	string filename;
	int positioncount = 0;
	int method = 0; 
	cout << "Enter the file  you want to count lines of code for(include extension): ";
	cin >> filename;
	file.open(filename.c_str());
	int remove = 0;

	bool bc = false;  
	// bc = block comment

	bool C = false;
	//c = class

	while(getline(file, line))
	{
		if(line.length()==0)
		{
			remove++;
		}
		else if(line.find("//") != std::string::npos && line.find("\"//\"") == std::string::npos)
		{
			remove++;
		}
		else if(line.find("/*") != std::string::npos && line.find("\"/*\"") == std::string::npos)
		{
			remove++;
			bc = true;
		}
		else if(line.find("*/") != std::string::npos && line.find("\"*/\"") == std::string::npos)
		{
			remove++;
			bc = false;
		}
		else if(line.find("{") !=std::string::npos && line.find("\"{\"") == std::string::npos)
		{
			remove++;
		}
		else if(line.find("}") !=std::string::npos && line.find("\"}\"") == std::string::npos)
		{
			remove++;
		}
		else if(bc)
		{
			remove++;
		}

		else positioncount++;	
	}


	while(getline(file, line))
		{
		if(line.find( "class") != std::string::npos && line.find("\"class\"") != std::string::npos)
		{
			method++;
			C = true;
		}
		else if(line.find("};") != std::string::npos && line.find("\"};\"") != std::string::npos)
		{
			method++;
			C= false;
		}
		else method++;
	}

	//cout << remove<< endl;
	cout << "There are " << positioncount << " lines of code." << endl;
	cout << method << endl;
	file.close();
	//cout << "test line" << endl;
}

int main()
{
	loc locObject;
	int val= locObject.counter();
	return 0;
}


I don't know how to get it to do what I want it to do.
is your problem trying to find objects? as much as i hate to admit, regexs + a container (stack or list maybe) might be the *easiest* option here
Methods and objects. And Would that mean modifying my whole code or starting from scratch?

ummm let me look through your whole code
Can anyone else provide some input into my problem? Thanks.
The first thing you need to do is define what you want to look for. I'm assuming you don't want a 'perfect' C++ parser (that would actually be incredibly difficult). So you just want to parse common/simple syntax, right?

So, ask yourself... "what constitutes a method?"

A basic C++ method has the following parts:

 
type methodname ( type param, type param ) ;


With the following caveats:

1) The whitespace before and after the parenthesis is optional
2) The semicolon will only exist for prototypes
3) The 'param' text is optional
4) The number of parameters is variable (ie, there might be nothing at all inside those parenthesis)


So maybe even this is too complex. Maybe you just want to look for this:

 
type methodname (


With this simplistic approach, the only thing you have to look for is the type, the method name, and an opening parenthesis (with optional whitespace before it). That'll do a decent job of catching most methods.




The really tricky part then.... is how do you recognize 'type'? Types may or may not contain whitespace... may or may not contain weird characters... and might even be custom types generated by the user.

ie:

1
2
3
4
5
6
7
8
9
10
11
12
int func();  // <- valid.. easy to recognize

MyClass func();  // <- possibly valid if user previously declared a class/struct
   // or type named 'MyClass'

std::string func();  // <- also valid.. trickier to recognize because of :: operator

std     ::     string func();  // <- also valid... much trickier to recognize

map< int, float   >  func();  // <- also valid!

const map< std :: string   ,MyClass*>& func(); // <- head explodey... but valid! 


So this problem can get quite complex.

Maybe you dont' want to bother recognizing all of these, and just want to recognize basic method definitions. If that's the case, you'll have to define your own rules for what will and won't be recognized as a method.

Then once you have the rules down... write your code to parse the text using those rules as a guide.
Last edited on
That post was great. Also made me realize how much more difficult this is. I guess this program has to work for only my coding standards.
Topic archived. No new replies allowed.