当试图找到'*'时,程序找到'.' 用c ++编写

0 c++ ascii char

所以我正在尝试编写一个代码来删除给定代码中的所有注释.给我的代码一个条件来检查当前char是'*'而下一个是'/'(注释结束)并尝试运行它,它接受./ as*/.

代码看起来像这样:

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];
    }
Run Code Online (Sandbox Code Playgroud)

所以当输入包含./时,我会继续收到"comment_nest--"

任何帮助,将不胜感激.谢谢.

m.a*_*icz 8

你有

    line[index == '*']
Run Code Online (Sandbox Code Playgroud)

而不是可能

    line[index] == '*'
Run Code Online (Sandbox Code Playgroud)

在附近的条件 comment_nest--;

上面几行相同的问题: line[index == '/']


C++接受它并且在编译时不会抛出错误,因为bool会自动转换为int.看一下这个帖子来获取更具体的信息: