Line count, what's wrong with my logic?

Okay I thought it was running well, but as I used different files, the line count screws up and give me wonky numbers (usually more.)

I want to exclude comments and empty lines.

I also did not implement mass comment either, as I wouldn't even know where to start with that O.O

Thank you for any help again guys!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
	
	while(getline(infile,lines)) 
	{

		if(lines.empty())  
			{
				continue;
					
			}
		if(lines.find("//"))
			{
				continue;
			}
		
		++linecount;
Last edited on
if(line.length() == 0)
By "spaces", you mean empty lines, right?

If a line is empty, you can test it with
if (lines_of_code.empty()).

In your case, I think you would actually want
1
2
if (!lines_of_code.empty())
    ++line_count;

(since you only want to increase the line count if the line's not empty).
^this

Also, getline removes newline characters, so you probably aren't getting anything.
Thank you everyone, I used the if statement LDM suggested and added my own condition for ignoring comments too. Works perfectly! Thank you so much.
Last edited on
Okay I am back again, please read original post that I edited. Thank you so much.
In real life, use sed/grep/awk ... | wc -l

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 <cctype>
#include <algorithm>

// ignores empty lines, and lines containg only comments or white space
// ignores lines containg only '{' or '}' other than spaces or a trailing comment

// does not take care of c-style /* .... */ comments
// does not take care of digraphs and trigraphs
// does not take care of compile-time optioning (#ifdef)
// does not count lines in included files

int main() // filter; reads from stdin and writes to stdout
{
    int line_cnt = 0 ;
    
    {{{   }}} // hello world!
    
    { // hello
    
    } // world
    
    /* this line is counted
       and this one too */
       
    ??< ??> // this too is counted
    
    #ifdef not_used // counted
        int x = 56 ; // counted
    #endif // not_used (counted)   

    std::string line ;
    while( std::getline( std::cin, line ) )
    {
        static const auto not_space = [] ( char c ) { return std::isspace(c) == 0 ; } ;
        
        // skip over leading white space
        auto iter = std::find_if( line.begin(), line.end(), not_space ) ;
        
        // take care of only '{' or '}'
        while( iter != line.end() && ( *iter == '{' || *iter == '}' || std::isspace(*iter) ) ) ++iter ;
        
        // not empty, and not a line with only a comment
        if( std::search_n( iter, line.end(), 2, '/' ) != iter )
        {
            ++line_cnt ;
            std::cout << line_cnt << ".\t" << line << '\n' ;
        }
    }

    // comment only line

    std::cout << "\n--------------\nline count: " << line_cnt << '\n' ;
}

http://coliru.stacked-crooked.com/a/0f1148e3250c7b64
> lines == "\n" == 0
¿what do you think you are doing?
That line checks for a newline character equal to 0.


Now that I think about it, the ==0 part should be removed?

But when I take that out, I don't get any values returned to linecount.

I am really confused now.
> checks for a newline character equal to 0.
¿what?

lines == "\n" would check that the line is just a `newline' character.
That can't happen as you are reading with `getline()'
Thanks I removed that, so that's what I have up there so far in my original post.

I can't seem to get the syntax correct for the program to correctly ignore the "//" comments.
Topic archived. No new replies allowed.