C++, Real-Time User Input, during While Loop

∥☆過路亽.° 提交于 2019-12-04 18:41:24

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

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.

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;
}

Hope this helps.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!