Counting Objects and methods

Hello, first time posting, thank you for your efforts in advance. I have a program right now that counts all of the lines of code excluding comments and blank lines. I was wondering how I would get it to count the number of methods and Object LOC? Any help would be appreciated
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
#include <iostream>
#include <string>
#include <fstream>
#include <stdio.h>

using namespace std;

class LOC{
    public:
        LOC ( string fname );   // constructor
    void countLines();
int returnNumber();  // will return the numLines,  needed because numLines is a private variable   
  
    private:
    string line, filename;
    int numLines;
};
LOC::LOC( string fname )
{
    filename = fname;
}

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

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

while (getline(myfile, line)) 
    {
        if(line[0] == '/' || line[0] == '\0' )   //ommit lines that are blank or have '/' at beginning
            {
                numLines--;    
            }        
        numLines++; //increments counter of number of lines
    }
}

int main()
{
    string fname = "main.cpp";

    LOC bo(fname); // this will create an LOC class, the constructor will fire up and will assign fname to bo’s ‘filename’ string.
    bo.countLines();
        
    cout << "The number of lines of code is: " << bo.returnNumber() << endl;
    return 0;
}
Last edited on
Can't you just count from class X{ to };? Then if they have an implementation file count every line?
Okay I have the program setup, and it runs but it puts out huge numbers for total LOC and methods, I need some help with why this is happening please and thank you.
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
#include <iostream>
#include <string>
#include <fstream>
#include <stdio.h>

using namespace std;

class LOC{
    public:
    LOC ( string fname )   // constructor
    {
        filename = fname;
    }

    int returnNumber()     // will return the numLines,  needed because numLines is a private variable   
    {
        return numLines;
    }

    int returnObjs()
    {
        return objlines;
    }

    int returnMethod()
    {
    return methodLines;
    }

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

        while (getline(myfile, line)) 
        {
            if(line[0] == '/' || line[0] == '\0')   //ommit lines that are blank or have '/' at beginning
                {
                    numLines--;   
                }

            if (line[0] != 'LOC')
                {
                    objlines--;
                }

            if(line[0] != 'void' || line[0] != 'int' && line[1] == 'r')
                {
                    methodLines--;
                }

            numLines++; //increments counter of number of lines
            objlines++;
            methodLines++;
        }
    }

    private:
    string line, filename;
    int numLines, methodLines, objlines;
};
/*LOC::LOC( string fname )
{
    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)) // Are we going to still need the bo.line?
    {
        if(line[0] == '/' || line[0] == '\0')   //ommit lines that are blank or have '/' at beginning
            {
                numLines--;   
            }        
        if (line[0] != 'LOC')
            {
                objlines--;
            }
        if(line[0] != 'void' && line[1] != 'LOC' || line[0] != 'int' && line[1] == 'LOC')
            {
                methodLines--;
            }
        numLines++; //increments counter of number of lines
        objlines++;
        methodLines++;
    }
}
*/
int main()
{
    string fname = "prog3a.cpp";

    LOC bo(fname); // this will create an LOC class, the constructor will fire up and will assign fname to bo’s ‘filename’ string.
    bo.countLines();
        
    cout << "The Total number of lines of code is: " << bo.returnNumber() << endl;
    cout << "The number of objects is: " << bo.returnObjs() << endl;
    cout << "The number of methods is: " << bo.returnMethod() << endl;
    return 0;
}
It might have something to do with the fact that you don't initialize your data members at all.
Aren't they being intialized by
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    int returnNumber()     // will return the numLines,  needed because numLines is a private variable   
    {
        return numLines;
    }

    int returnObjs()
    {
        return objlines;
    }

    int returnMethod()
    {
    return methodLines;
    }
Okay, well now I am getting the code to print correctly the total number of lines, but it is printing every single line instead of omitting comments '/' or empty line '\0' whats wrong with my if statement?

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

using namespace std;

class LOC{
    public:
    LOC ( string fname )   // constructor
    {
        filename = fname;
    }

    int returnNumber()     // will return the numLines,  needed because numLines is a private variable   
    {
        return numLines;
    }

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

        while (getline(myfile, line)) 
        {
            if(line[0] == '/' || line[0] == '\0')   //ommit lines that are blank or have '/' at beginning
            {
                numLines--;   
            }

            else
            {
                 numLines++; //increments counter of number of lines
            }
        }
    }

    private:
    string line, filename;
    int numLines;

};   // class ends

int main()
{
    string fname = "prog3a.cpp";

    LOC bo(fname); // this will create an LOC class, the constructor will fire up and will assign fname to bo’s ‘filename’ string.
    bo.countLines();
        
    cout << "The Total number of lines of code is: " << bo.returnNumber() << endl;

    return 0;
}
As cire mentioned earlier you never initialized numLines. You are going to want to initialize it to 0 in the constructor.
1
2
3
4
5
6
7
8
class LOC{
    public:
    LOC ( string fname )   // constructor
    {
        numLines = 0;
        filename = fname;
    }


I am still only getting total lines of code without omitting the comments or blank lines. I do not want to count comments or blank lines as code but it still is unfortunately.
Comments rarely occur at the very beginning of a line. You do not count blank lines (however, you would count lines that have at least one non-newline white space character.) In fact, you reduce the line count for empty lines.

1
2
3
{

}


would result in a line count of 1 with the last code you posted.
Okay, so I have gotten it working as far as counting total LOC. Giblit mentioned to count everything within
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
class LOC{
    public:
    LOC ( string fname )   // constructor
    {
        filename = fname;
    }

    int returnNumber()     // will return the numLines,  needed because numLines is a private variable   
    {
        return numLines;
    }

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

        while (getline(myfile, line)) 
        {
            if(line[0] == '/' || line[0] == '\0')   //ommit lines that are blank or have '/' at beginning
            {
                numLines--;   
            }

            else
            {
                 numLines++; //increments counter of number of lines
            }
        }
    }

    private:
    string line, filename;
    int numLines;

}; 


How would I do this? Because I am trying to check for the number of objects, how many lines total are those objects, and the number of methods, with there number of lines total... I am starting to see code when I sleep lol.
Topic archived. No new replies allowed.