C ++,实时用户输入 [英] C++, Real-Time User Input, during While Loop

查看:234
本文介绍了C ++,实时用户输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用户有任何方式提供实时输入,而某些内容在后台不断更新。基本上,当要求用户输入时,使程序不停止。

Is there was any way for the User to give a Real-Time input, while something is constantly being updated in the background. Basically, making the program not stop, when asking for user input.

例如,
它将要求用户输入, 。

For example, It will ask for user input, while a number is constantly being calculated.

推荐答案

这个问题有两种方法,就像我看到的。

There are two ways of this problem, as I see it.

一个是,正如xebo所说,使用多线程。

One is, as xebo commented, using multi threading. Use one thread for the constant calculation of the number or whatever, and another thread to look for user input constantly.

第二种方法是更简单的,只有当你使用一个线程来常数计算数字,正在使用cin(从std命名空间)
来获取用户输入。您可以在计算循环内嵌入另一个while循环,如下所示:

The second method is a simpler on and works only if you are using cin( from the std namespace) to get user input. You can nest another while loop inside the calculation loop like this:

#include <iostream>
using namespace std;

int main()
{
    int YourNumber;
    char input;         //the object you wish to store the input value in.
    while(condition)    //Whatever your condition is
    {
        while(cin>>input)
        //This while says that the statement after (cin»input)
        //is to be repealed as long as the input operation 
        //cin>>input succeeds, and
        //cin»input will succeed as long as there are characters to read 
        //on the standard input.
        {
             //Update process your input here.
        }
        //D what the normal calculations you would perform with your number.
    }
return 0;
}

希望这有帮助。

这篇关于C ++,实时用户输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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