当尝试查找'*'程序在 C++ 中查找'.'
when trying to find '*' the program finds '.' insted in c++
所以我正试图编写一个代码,删除给定代码中的所有注释。当给我的代码一个条件来检查当前字符是"*",下一个字符是"/"(注释末尾)并尝试运行它时,它接受了。/作为*/。
代码看起来像这样:
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的转换
相关文章:
- 正在查找文档以获得PS4平台的C++中的设备信息
- 在C++中查找文件
- 模板元程序查找相似的连续类型名称
- 在UNIX系统中使用DIR查找文件的字节大小
- 查找最接近的大于当前数字的数字的索引
- 有没有一种方法可以创建一个带有哈希表的数据库,该哈希表具有恒定时间查找功能
- 查找后更改类变量
- 使用正则表达式regex_search在字符串中查找字符串
- 使用gcc从静态链接的文件中查找可选符号
- 在C++中查找范围的长度
- 算法问题:查找从堆栈中弹出的所有序列
- 在Windows中查找扬声器输出的当前音量级别
- 如何在C++中使用X509证书模在令牌中查找私钥
- 使用.find函数在c++中查找字符和另一个字符之间的大小
- 在 Windows 上,是否可以让 dll 在不使用 PATH 环境变量的情况下在另一个文件夹中查找依赖项?
- 在 for 循环中查找问题时遇到困难
- 如何在文件中查找字节序列
- 试图创建一个多线程程序来查找0-100000000之间的总素数
- 使用堆查找第K个最大元素的时间复杂性
- 一个函数,用于查找字符串1包含字符串2 c++的次数