当尝试查找'*'程序在 C++ 中查找'.'

when trying to find '*' the program finds '.' insted in c++

本文关键字:查找 C++ 程序      更新时间:2023-10-16

所以我正试图编写一个代码,删除给定代码中的所有注释。当给我的代码一个条件来检查当前字符是"*",下一个字符是"/"(注释末尾)并尝试运行它时,它接受了。/作为*/。

代码看起来像这样:

char line[MAX_LINE_LEN] = { 0 };
...Input and some code...
  for (int index = 0; index < MAX_LINE_LEN - 1 && line[index] != '';
            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];
    }

因此,当输入包含"comment-nest--"时,我会一直得到"comment_nest--"。/

如有任何帮助,我们将不胜感激。谢谢

你有

    line[index == '*']

而不是

    line[index] == '*'

comment_nest--; 附近的条件下

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


C++接受它,并且在编译时不会抛出错误,因为自动将bool转换为int。看看这个线程可以获得更具体的信息:

  • 布尔到int的转换