Ctrl+Z无法在循环时退出
Ctrl + Z not working to exit while loop
我一直在四处寻找并尝试了很多方法,但我一辈子都无法让Ctrlz退出控制台。这是我的代码,有人能告诉我正确的方向吗:
这是在Windows上。
int main()
{
string s;
Stack *stack = new Stack();
while (cin >> s)
{
if (cin)
{
for (int i = 0; i < (int)s.length(); i++)
{
stack->push(s[i]);
}
for (int i = 0; i < s.length(); i++)
{
cout << stack->top();;
stack->pop();
}
cout << endl;
}
}
stack->~Stack();
delete stack;
return 0;
}
在Windows上,按Ctrl-Z以发出EOF信号。在Linux上,它是Ctrl-D。在任何一个系统上,您都需要在行首按下它(即,在按下回车键后)。
顺便说一下,没有必要显式调用析构函数。去掉这条线:
stack->~Stack();
事实上,最好是完全去掉new
和delete
。这些闻起来像Java主义。在C++中,您不必总是使用new
来创建新对象。更基本的语法是写:
Stack stack;
这将创建一个Stack
对象,并调用默认的无参数构造函数。当main()
退出时,对象将被自动销毁,因此您不需要delete
或显式的销毁函数调用。
最后,if (cin)
检查是多余的。while
循环已经检查是否读取了字符串。
while (cin >> s)
{
if (cin)
{
...
}
}
由于您使用的是内置堆栈,因此不需要使用。->并且如果(cin)或者作为都不需要,而(cin>>s)is检查用力流是否已到达终点。这是正确的代码。
#include <string>
#include <stack>
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
string s;
stack <string> st;
while (cin >> s)
{
st.push(s);
cout << st.top();;
st.pop();
cout << endl;
}
st.~stack();
return 0;
}
相关文章:
- C++ 自动点击器未退出循环
- 使用 while 或 do while 退出循环
- 退出循环,除非未给出输入
- C++线程退出循环条件
- 在C++测试家庭作业问题中的字符串输入时无法退出循环(作为数据验证)
- 虽然具有多个 OR 条件的循环不退出循环
- 如果程序退出循环是goto已删除的本地变量
- 中断语句未退出循环
- 计算平均值,而不向退出循环添加负等级
- 使用键盘挂钩时无法动态退出循环
- 定义时间后退出C 循环
- 使用goto来干净地退出循环
- getchar 有什么问题(因为程序在没有检查的情况下退出循环)
- 尝试退出循环时,Ctrl+Z 不起作用
- 如何获取用户输入以退出循环
- 正在销毁线程并退出循环
- 中断语句不会退出循环
- 通过输入一个特定的字母c++来退出循环
- 在c++中,当输入不是unsigned时立即退出循环
- 编程原理和实践使用C++while-loop演练。找不到退出循环的方法