按下键时停止C ++中的无限循环 [英] Stopping an infinite loop in C++ when key is pressed

查看:128
本文介绍了按下键时停止C ++中的无限循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下所示的程序:

I have a program as given below:

#include<iostream>
using namespace std;
int main()
{
    while(true)
    {
        //do some task
        if(Any_key_pressed)
            break;
    }
return 0;
}

如果按下任何键,如何退出循环.

how can exit from the loop if any key is pressed.

C++ Compiler: GCC 4.2 and higher
OS: Linux-Mint

谢谢

推荐答案

标准C ++没有提供实现此目的的方法.您将需要一个特定于平台的API,该API可以告诉您是否按下了键(或在stdin上可以使用输入)而没有阻塞.这意味着您需要告诉我们您正在使用哪个平台.

Standard C++ doesn't offer a way to do this. You will need a platform-specific API that tells you whether a key was pressed (or input is available on stdin, possibly) without blocking. Which means you need to tell us which platform you're using.

这篇关于按下键时停止C ++中的无限循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆