How do I count the lines for the method and object. I can't figure it out.

Pages: 12
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
#include <iostream>
#include <string>
#include <fstream>
#include <stdio.h>

using namespace std;

class LOC{

public:
	// constructor
	LOC (string fname); 
	void countLines();
	void countOLines();
	void countMLines();
	//return numLines
	int returnNumber();
	int returnObjs();
	int returnMethod();

private:
	string line, filename;
	int numLines, objlines, methodLines;
};

LOC::LOC(string fname){
	numLines = 0;
	objlines = 0;
	methodLines = 0;
    filename = fname;
}

int LOC::returnNumber(){
    return numLines;
}
int LOC::returnObjs(){
	return objlines;
}

int LOC::returnMethod(){
	return methodLines;
}

void LOC::countLines(){
    ifstream myfile(filename.c_str());

	while (getline(myfile, line)){
		//ignore lines that are either blank or /
		if(line.empty() || (line.find("//")!=line.npos)){
			continue;
		}        
		//increment lines of number count
        numLines++; 
    }
	myfile.close();
}

void LOC::countOLines(){
	ifstream myfile(filename.c_str());
	while(getline(myfile, line)){
		if(line.find("class") !=line.npos){
			objlines++;	
		}  
	}
	myfile.close();
}


void LOC::countMLines(){
	ifstream myfile(filename.c_str());
	while(getline(myfile, line)){
                if(line.find("::") !=line.npos){
			methodLines++;	
          	}
	}
	myfile.close();	
}

int main()
{
    string fname;
	cout <<"Enter file name: " << endl;
	cin >> fname;

	LOC a(fname);
	LOC b(fname);
	LOC c(fname);

	a.countLines();
	b.countOLines();
	c.countMLines();

    cout << "There are " << a.returnNumber() << " lines of code in this file." << endl;
    cout << "There are " << b.returnObjs() << " object lines of code in this file." << endl;
    cout << "There are " << c.returnMethod() << " method lines of code in this file." << endl;
	return 0;
}
Last edited on
You're searching filename for the strings when you should be searching line.
I deleted the filename, but I don't know what to do. I'm an amateur at this.
No you don't delete it, you replace it.
See, filename.find() searches only the file's name, not the contents.
To search the contents, you use the string you passed in the second argument for getline(myfile, line).
Se replace filename.find() with line.find()
Last edited on
Okay. I redid my code with line.find() but I still cant figure it out.
1
2
3
4
5
6
7
8
9
10
void LOC::countOLines(){
	ifstream myfile(filename.c_str());
	while(getline(myfile, line)){
		if(line.find('class') == true)&& (filename.find('};') ==false)){
				continue;
		}   
		objlines++;	
	}
	myfile.close();
}
What is it supposed to do and what isn't it doing?
It suppose to count the number of object the program contains and the number of methods in each object.
Ok, right now it's ignoring lines containing 'class' or '::' and counting everything else for object and method lines, right?
If you just want it to count when it encounters the string 'class' or '::' then you need to move objlines++ and methodlines++ into their respective if statements before the continue.
I don't understand. So I just remove continue then?
If I understand you correctly, you want to count how many objects and methods there are but NOT how many lines of code make them up, right?
If that's true, then yes, lose the continue and put the counter inside the if statement so it only counts when it encounters the string 'class' and '::'.
Okay I tried that but it read class twice an none for ::.
Post what you have now, or edit your code in the OP.
///
Last edited on
Oh, it counts class twice because it finds it twice. Once in the class declaration and once in the find function. That's a tough one. If you're searching this source file, no matter what you do you will always find one extra of what you're looking for.
I don't know, maybe someone more experienced than me will come along and help.

Also, I just noticed this, but find doesn't return a boolean value so use if(line.find() != line.npos) instead. And double quotes for a string, single quotes for a character.
Alright, thanks for your help. Still couldn't figure it out though.
Is there other ways to count the loc for the object class and methods in the class?
I think you're going to need to implement some kind of lexical analyzer. I don't see how it could be done in an easier, but admittedly I don't have a lot of experience in this area.
http://www.tutorialspoint.com/compiler_design/compiler_design_lexical_analysis.htm

Line 47 blows away filename. So when you try to open the file again at line 59, filename contains junk (specifically the last line of the program).

Line 49: '//' should be "//" (double quotes, not single). Also, what if the line is a = 1; // blah blah blah ? Without additional code, this will register as a comment, even though it also contains code.
What do I put instead of filename?
For the
class
and the
::
do I put
""
as well?
Last edited on
Do I delete
ifstream myfile(filename.c_str());
on Line 47?
Last edited on
Pages: 12