c++ - 当试图找到 '*' 程序发现 '.' insted in c++

标签 c++ char ascii

<分区>

所以我正在尝试编写一个代码来删除给定代码中的所有注释。 当给我的代码一个条件来检查当前字符是否为“*”而下一个字符是否为“/”(注释结束)并尝试运行它时,它接受 ./作为 */。

代码看起来像这样:

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];
    }

所以当输入包含 ./时,我不断收到“comment_nest--”

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

最佳答案

你有

    line[index == '*']

而不是可能

    line[index] == '*'

comment_nest--;附近的情况

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


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

关于c++ - 当试图找到 '*' 程序发现 '.' insted in c++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36480713/

相关文章:

c++ - Debian 上的 GLFW 回调 glfwSetDropCallback 有问题

python - 为 32 位、64 位和 128 位生成交错位模式(莫顿 key )

在 C 中复制 char*

java - 如何使用 Netty 在单个端口上编写多协议(protocol)处理程序?

javascript - 解码或转义字符串中的十六进制代码数字

c++ - 错误:预期的类名(链接列表C++)

c++ - #定义一个模板方法

java - 字符转字节? ( java )

java - 打印不重复的字符元素

java - 共享网 : How to send a file over FTP using ASCII without changing LF to CRLF