新手求助,有关按esc退出的问题

pcajax 2013-05-22 10:16:20
如题,我编了一个简单的求和程序,现在想改进成按esc(或者任何一个特定键,)退出程序,如果不是特定键,就重复进行计算,直到输出的是特定键为止。PS:如果输入的不是特定键,输入应直接当做数值传入,而不是一个简单的判断输入。代码如下:
#include<iostream>
using namespace std;
int main()
{

while(1)
{
float a,b,t;
cout<<"input the number:";
cin>>a>>b;
t=a+b;
cout<<"the result is:"<<t<<endl;
}
return 0;
}
...全文
128 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
番茄-梁小魁 2013-05-26
  • 打赏
  • 举报
回复
学习学习,谢谢指导
ForestDB 2013-05-24
  • 打赏
  • 举报
回复
刚开始还是选择'q'退出的比较好。 Esc这样的东西,已经开始和平台的东西相关了。
赵4老师 2013-05-24
  • 打赏
  • 举报
回复
//The _getch function reads a single character from the console without echoing.
//Function can not be used to read CTRL+Break.
//When reading a function key or an arrow key,
//_getch must be called twice; the first call returns 0 or 0xE0,
//and the second call returns the actual key code.
#include <conio.h>
#include <windows.h>
void main() {
    unsigned short k;

    while (1) {
        Sleep(100);
        if (_kbhit()) {
            k=getch();
            if (27==k) break;//按Esc键退出
            if (0==k||0xe0==k) k|=getch()<<8;//非字符键
            cprintf("\r\n%04x pressed.\r\n",k);
        } else cprintf(".");
    }
}
lunat 2013-05-22
  • 打赏
  • 举报
回复
getche
www_adintr_com 2013-05-22
  • 打赏
  • 举报
回复
开个线程, 里面不停的用 GetKeyboardState 来检查键盘的状态. 如果检查到对应的按键被按下, 就 exit

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧