C++ - 简单 - 嵌套,而循环永远不会终止
C++ - Simple - nested while loops never terminate?
我只是在修改一些文件的 I/O C++。编写一个程序,将自己的源代码打印到终端而不带注释
以下是有问题的循环 使用 if 语句
while (!inputstream.eof())
{
if(in_comment == false)
{
inputstream.get(temp);
if(temp == '/')
{
inputstream.get(temp1);
if (temp1 == '*')
{
in_comment = true;
}
else
{
inputstream.putback(temp1);
}
}
cout << temp;
}
if(in_comment == true)
{
inputstream.get(temp);
if(temp == '*')
{
inputstream.get(temp);
if (temp == '/')
{
in_comment = false;
}
}
}
}
在这里,他们不使用while循环
while (!inputstream.eof())
{
while(in_comment == false)
{
inputstream.get(temp);
if(temp == '/')
{
inputstream.get(temp1);
if (temp1 == '*')
{
in_comment = true;
break;
}
else
{
inputstream.putback(temp1);
}
}
cout << temp;
}
while(in_comment == true)
{
inputstream.get(temp);
if(temp == '*')
{
inputstream.get(temp);
if (temp == '/')
{
in_comment = false;
}
}
}
}
我本来希望 eof 标记会导致程序脱离外部 while 循环,但它没有。这是为什么呢?
谢谢
你的内部循环不会在 eof 上中断,所以你会得到一个无限循环 - 就这么简单。只有当内循环离开时,外循环才有机会中断。工作示例没有内部循环,因此外部循环可以结束。
相关文章:
- OpenGL - 在抛出"__gnu_cxx::recursive_init_error"实例后终止调用?
- 多个文件的内存分配错误"在抛出 'std :: bad_alloc' what (): std :: bad_alloc 的实例后终止调用" [C++]
- 我收到以下错误:抛出'std::bad_alloc'实例后终止调用
- 为什么这个 c++ 代码打印出长度 5,当我打印出字符串时,程序会自动终止?
- 当我在其中一个线程执行中(在activemq-cpp中)捕获到特定值时,我如何终止/停止所有其他线程
- llvm构建器向基本块添加终止符
- 为什么是谷神星协方差.计算()似乎永远运行而不返回?
- 如何使用隔离>终止执行来停止所有线程
- 密码登录程序将永远循环并显示不正确的结果
- 用于将C++代码转换为 Web 程序集的脚本未终止
- C++应用程序 MySQL odbc 数据库连接错误:在引发"otl_tmpl_exception<>"实例后终止调用
- 终止 QProcess 不会终止子进程
- 我想在C++中读取一些多个字符,但它永远不会读取第二个字符
- SFML 碰撞永远不会在我的系统中注册
- 运行代码时,c++ 会终止进程
- 未定义的行为怪癖:在缓冲区外读取导致循环永远不会终止
- 如果功能永远不会终止,则记忆的交易单位
- CIN循环永远不会终止
- 如何使我的C++程序永远等待传感器事件而不终止
- C++ - 简单 - 嵌套,而循环永远不会终止