1

I'm writing a small program that count the lines of code. Here is the definition of a line of code: - any line that contains code necessary for the program to function. - blank line is not a line of code. - comment is not a line of code. - if there's code and a comment right after on the same line, it counts as well.

So I have this piece of code (simple if statement):

found = lineRead.find("/*");
if(found != string::npos)
{
    found = lineRead.find("*/");
    if(found != string::npos)
        inComment = false;
    else
        inComment == true;
}
return inComment;

Assume that

String lineRead = "cout<<\"helloworld!\";/*blockcomment"
Bool inComment (is true if the previous line didn't have end block comment token)

So my program reaches the first if statement because it found /* in that line, looks for */, goes to the second if statement then jumps straight to the return statement without changing inComment (which is supposed to be set to true because the text on the next line is still inside the block comment).

Anyone know why that is?

6
  • Have you tried using a debugger? The problem is staring you in the face. Really? May 28, 2014 at 18:08
  • The if/else could be replaced by inComment = (found == string::npos); May 28, 2014 at 18:13
  • @KeithThompson: Or return (found == string::npos);. May 28, 2014 at 18:32
  • 1
    @staticx: Which raises another point: What's the value of inComment if the outer if condition is false? May 28, 2014 at 18:47
  • the problem was fixed. Thank you! I'm exhausted and tired. I just missed this very silly mistake.
    – MB41
    May 28, 2014 at 18:55

2 Answers 2

9

Your problem is the infamous "double equal sign."
inComment == true;

In C++, == is used for compare, NOT for assignment. I think what you want is: inComment = true;

1
  • boy do I feel stupid...I pulled an all-nighter. Really can't think straight apparently.
    – MB41
    May 28, 2014 at 18:16
0

Double equal sign is for comparison not assignment. Probably just a typo as I assume you know that. But yea, change == to just = :-)

Not the answer you're looking for? Browse other questions tagged or ask your own question.