如何通过按特殊字符"!"在 c++ 上终止我的程序?

How could I terminate my program on c++ by pressing the special character '!'?

本文关键字:终止 我的 程序 c++ 何通过 特殊字符      更新时间:2023-10-16

所以我正在为我的类分配制作一个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);
相关文章: