如何通过按特殊字符"!"在 c++ 上终止我的程序?
How could I terminate my program on c++ by pressing the special character '!'?
所以我正在为我的类分配制作一个c++计算器,我需要能够通过按下特殊字符退出程序。我尝试过do/while循环、while循环,甚至库windows.h中的GetAsyncKeyState
,但似乎都不起作用。我的程序的主要功能看起来像这样:
int main()
{
Stack<char> a;
char * expression;
expression=new char[30];
char * temp;
temp=new char[30];
int result=0;
bool flag=true;
cout<<"Enter an Infix Expression:";
cin>>expression;
//some more code//
.....
}
有没有办法每当特殊字符,将其存储在char变量表达式中并退出程序?或者每当按下该键时,我该怎么退出程序?
对不起,我是新来的,我不知道是否可以在注释中写代码,所以我只写了一个新的答案
while(true)
{
cout<<"Enter an Infix Expression:";
cin>>expression;
if(string(expression).at(0) == '!')
break;
... //do your things here
}
exit(0);
//try this, and tell me if it works. I think this is better than the last one
您必须实现自己的"从stdin读取内容"函数,该函数将监视!
并相应地采取行动(可能通过直接调用exit()
或抛出异常)。我会提供更多信息,但你的问题没有要求具体细节。此外,您是如何解析表达式的?我问beause !
恰好是阶乘运算符,您的用户可能会输入它。
你试过这个吗?
if(strcmp(expression, "!") == 0)
exit(0);
相关文章:
- 如何改进我的代码,使其不会因超时而终止?(黑客排名挑战)
- 为什么我的 while 函数没有终止?
- 为什么我的显示功能会终止程序?
- PostQuitMessage(0) 不会终止我的"程序"
- 这是我的 .h 文件的一些错误,当我在其中包含我的类模板时,它显示 [错误] 未终止 #ifndef
- 当我输入非整数值时,我的二次方程代码终止,如何让它正确循环?
- 为什么我的 C++ 应用以退出值 -1 终止,但某些代码逻辑未执行?
- 我正在尝试计算 char 数组中的内容,直到 null 终止,但每次编译时,我都会得到一个比我的数组大的数字
- 为什么我的析构函数中的异常没有触发 std::终止?
- 尝试将未知数量的字符串推送到向量中,但我的 cin 循环没有终止
- 我的链接列表程序在代码块编译器(X0000000005)中以状态-1073741819终止
- 对我是否需要一个哨兵值来终止我的 do while 循环以允许多个条目感到困惑
- 为什么我的 c++ 程序在 Eclipse 中没有输出而终止,但在 Visual Studio 中工作
- 为什么我的程序在执行后立即终止
- 为什么我的简单C++项目在我可以看到之前就终止于日食?我如何让它停止
- 我的Qt C++程序以一种不同寻常的方式终止
- 当我采用测试用例数 = 1 时,我的程序终止.其他明智的是,它给出了正确的答案,但跳过了最后一个测试用例
- 当我的程序终止时,我应该总是在COM指针上调用Release吗
- 如何通过按特殊字符"!"在 c++ 上终止我的程序?
- 我如何终止我的程序使用ESC按钮