• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

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

legcramp

Golden Member
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!
 
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?
 
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 ...........
 
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.
 
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.
 
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.
 
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.
 
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!
 
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.
 
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!
 
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 🙁
 
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.
 
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++;
}
 
Back
Top