C++ how to ignore comments "//" in line count!

legcramp

Golden Member
May 31, 2005
1,671
113
116
Code:
while(getline(infile,lines)) 
	{

		if(lines.empty())  
			{
				continue;
					
			}
		if(lines.find("//") == true)
			{
				continue;
			}
		
		++linecount;


I got the counter to skip spaces flawlessly, but skipping the comments in the line count isn't going very well.

Any help is appreciated!
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
Hmm, don't see anything obviously wrong, but in general you haven't given enough information to enable someone to help. What erroneous behavior or results are you seeing?
 

legcramp

Golden Member
May 31, 2005
1,671
113
116
Sorry, forgot to mention that I just want the counter to skip the line if the "//" is in front of the line.

Right now, the line is skipped for example:

cout << "Hello World" << endl; //comment here


But I only want it to skip the line if:

//comment here ...........
 

BoT

Senior member
May 18, 2010
365
0
86
www.codisha.com
why would you want to do that? it is editor dependent not code dependent. there is nothing in C#, C+ or C++ that would make the editor skip count. it's an interpretation of the editor of how to present what you wrote in code.
 

Merad

Platinum Member
May 31, 2010
2,586
19
81
You're ignoring the line if // appears anywhere in it. Instead of searching the whole string you need to see if it begins with //. Ideally, strip whitespace from the start of the string before checking.

Also, assuming what you posted is actual code, you're using the find method incorrectly: http://www.cplusplus.com/reference/string/string/find/
 

purbeast0

No Lifer
Sep 13, 2001
53,475
6,316
126
string:find does not return a boolean. that is why it's never gets inside of that second conditional. it returns the index of where it finds the parameter inside of the string.

you're basically comparing an integer == true.

make it compare to -1 (which it returns if it finds nothing) instead of true.
 

Crusty

Lifer
Sep 30, 2001
12,684
2
81
string:find does not return a boolean. that is why it's never gets inside of that second conditional. it returns the index of where it finds the parameter inside of the string.

you're basically comparing an integer == true.

make it compare to -1 (which it returns if it finds nothing) instead of true.

Well, really you should use string::npos to compare against.
 

purbeast0

No Lifer
Sep 13, 2001
53,475
6,316
126
Well, really you should use string::npos to compare against.

been forever since i've done c++ so sorry if i missed that :)

i was just going off of my general knowledge of when doing finds on strings in general, it always returns the index if it finds it and -1 if it finds nothing.
 

mjd

Member
Jan 3, 2007
135
0
76
That code isn't going to skip non-empty blank lines either.
Suppose your example data has a blank line with spaces or tabs or other whitespace in it!
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
That code isn't going to skip non-empty blank lines either.
Suppose your example data has a blank line with spaces or tabs or other whitespace in it!


Good point. A simple approach would be:

for each line,
- strip leading whitespace
- skip if the stripped string empty or the first 2 characters are "//"

This won't work for /* ... */ blocks of course, but that probably isn't asked for in the homework.
 

mjd

Member
Jan 3, 2007
135
0
76
Yeah, there are a bunch of ways to solve this problem. And it really depends on the intent of the homework question and tools available.

character-level string processing,
Boost style trim and matching,
C++11 regex module use,
or more!
 

Crusty

Lifer
Sep 30, 2001
12,684
2
81
Yeah, there are a bunch of ways to solve this problem. And it really depends on the intent of the homework question and tools available.

character-level string processing,
Boost style trim and matching,
C++11 regex module use,
or more!

Does that work in G++ yet?

Last time I tried to use it, everything compiled and linked just fine, but the library just returned completely bogus results. Turns out the version of G++ I had didn't support it yet :(
 

mjd

Member
Jan 3, 2007
135
0
76
Does that work in G++ yet?

Last time I tried to use it, everything compiled and linked just fine, but the library just returned completely bogus results. Turns out the version of G++ I had didn't support it yet :(

A cursory glance at the internet tells me that g++ regex is still broken as of December 2013. I haven't tried it myself.
 

MrScott81

Golden Member
Aug 31, 2001
1,891
0
76
Here's how I would do it in java:
Code:
String line=null;
int linescount=0;
while( (line = in.readLine()) != null)
{
  if(line.trim().equals("") || line.trim().startsWith("//"))
    continue;
  linescount++;
}