Line Counter Program - Inaccurate Count

Can someone help me with my logic? I'm trying to output the number of lines inside of my class starting from the class name down to the ending bracket. The count should be 5 based on this code but it is spitting out 9. I can't see where my mistake is and I'd appreciate help locating where I need to look to fix this. Any help is 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
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
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <typeinfo>

using namespace std;

string loc;
string current;
string class_name;
int class_loc = 0;
int lines = 0;
int method_count = 0;
bool isComment = false;

class LOC {
        public:
                LOC();
                int get_LOC();  
};

LOC::LOC() {
}

int LOC::get_LOC() {
        // get class name and pass into string
        class_name = typeid(LOC).name();

        // create input file
        ifstream infile;

        // get input file from user
        cout << "Enter the filename" << endl;
        cin >> loc;

        // open the input file
        infile.open(loc.c_str());

        // until the end of the file is reached, search for comments and blank lines
        while(!infile.eof()) {
                getline(infile, current);

                if(current.find("//") != std::string::npos && current.find("\"//\"") == std::string::npos){
                        isComment = true;
                }
                else{
                        isComment = false;
                }
                 if(!current.empty() && !isComment) {
                        lines++;
                }

                if(current.find("class") != string::npos) {
                        class_loc++;
                        if(current.find(";}") != string::npos) {
                                break;
                        }
                }

                if(current.find("public") != string::npos) {
                        method_count++;
                        if(current.find("};") != string::npos) {
                                method_count = method_count - 2;
                                break;
                        }
                }
        }        
        // close the input file
        infile.close();

        // display the results
        cout << "The names of the classes: " << class_name << endl;
        cout << "There are " << class_loc << " lines of code in each class." << endl;
        cout << "There are " << method_count << " methods in this file." << endl;
        cout << "There are " << lines << " logical lines of code in this file." << endl;
}

int main() {
        LOC line_Counter;
        line_Counter.get_LOC();
}
The count should be 5 based on this code
Based on this code, I see that you want to count all lines with "class" in them. There are nine ot those:
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
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <typeinfo>

using namespace std;

string loc;
string current;
string class_name;
int class_loc = 0;
int lines = 0;
int method_count = 0;
bool isComment = false;

class LOC {
        public:
                LOC();
                int get_LOC();  
};

LOC::LOC() {
}

int LOC::get_LOC() {
        // get class name and pass into string
        class_name = typeid(LOC).name();

        // create input file
        ifstream infile;

        // get input file from user
        cout << "Enter the filename" << endl;
        cin >> loc;

        // open the input file
        infile.open(loc.c_str());

        // until the end of the file is reached, search for comments and blank lines
        while(!infile.eof()) {
                getline(infile, current);

                if(current.find("//") != std::string::npos && current.find("\"//\"") == std::string::npos){
                        isComment = true;
                }
                else{
                        isComment = false;
                }
                 if(!current.empty() && !isComment) {
                        lines++;
                }

                if(current.find("class") != string::npos) {
                        class_loc++;
                        if(current.find(";}") != string::npos) {
                                break;
                        }
                }

                if(current.find("public") != string::npos) {
                        method_count++;
                        if(current.find("};") != string::npos) {
                                method_count = method_count - 2;
                                break;
                        }
                }
        }        
        // close the input file
        infile.close();

        // display the results
        cout << "The names of the classes: " << class_name << endl;
        cout << "There are " << class_loc << " lines of code in each class." << endl;
        cout << "There are " << method_count << " methods in this file." << endl;
        cout << "There are " << lines << " logical lines of code in this file." << endl;
}

int main() {
        LOC line_Counter;
        line_Counter.get_LOC();
}
So, i think you want to see how many Lines of Code that class has:

1
2
3
4
5
17: class LOC {
18:         public:
19:                 LOC();
20:                 int get_LOC();  
21: };


but atm you search for the word "class" and when found you increase the counter which results in giving you the amount of the word "class" in the File.

Try this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int IsClass = 0;

while(!infile.eof())
{
    getline(infile, current);

   // ...

    if(current.find("class LOC") != string::npos)
        IsClass = 1;

    if(current.find("};" != string::npos)
        IsClass = 0;

    if(IsClass)
        class loc++;
}


I hope i got your objective right, but I'm tired as hell and I'm going to sleep now.

Greetings,
Gamer2015
Gamer2015 you got the concept right but this code only fixes the count by 1. Instead of returning 9 for the class line count, it returns 8. I'm still unsure what I'm doing wrong.
oh yeah sure, you have to count before checking if the class has ended ^^
You want to count the ending bracket too, right?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int IsClass = 0;

while(!infile.eof())
{
    getline(infile, current);

   // ...

    if(current.find("class LOC") != string::npos)
        IsClass = 1;

    if(IsClass)
        class loc++;

    if(current.find("};" != string::npos)
        IsClass = 0;
}


but shouldn't you get 4 or 5 as result rather than 8 or 9?
Topic archived. No new replies allowed.