0

So I'm trying to write a code that deletes all comments from a giving code. When giving my code a condition to check if the current char is '*' and the next one is '/' (end of comment) and try to run it, it accepts ./ as */.

The code looks something like that:

char line[MAX_LINE_LEN] = { 0 };

...Input and some code...

  for (int index = 0; index < MAX_LINE_LEN - 1 && line[index] != '\0';
            index++)
    {
        if (line[index] == '/' && line[index + 1] == '/'
                && comment_nest == 0)
            break;
        if (line[index == '/'] && line[index + 1] == '*')
            comment_nest++;
        if (line[index == '*'] && line[index + 1] == '/')
            comment_nest--;

        if (comment_nest == 0)
            cout << line[index];
    }

so i keep getting "comment_nest--" when the input contains ./

Any help would be appreciated. Thanks.

1 Answers1

8

You've got

    line[index == '*']

instead of probably

    line[index] == '*'

in the condition near comment_nest--;

The same problem few lines above: line[index == '/']


C++ accepts it and do not throw error while being compiled because of automatic conversion bool to int. Take a look at this thread to get more specific information:

Community
  • 1
  • 1
m.antkowicz
  • 13,268
  • 18
  • 37