How do you send an input into a continuous loop? (C++)
I have a problem, lets say I have a continuous while loop, and inside there is a v开发者_StackOverflowariable x that is used in some calculation. How do I send an input from the keyboard to the loop to change the variable x within the loop without stopping the loop in C++? Thanks
Your best bet is probably multi-threading, a second thread could wait for a keyboard.
Have a look at boost/c++ for details of implementation : http://www.boost.org/doc/libs/1_43_0/doc/html/thread.html
If you're running linux, your best bet is using ncurses. The S-lang library is also used by some, but I have no experience with it. Multithreading is not required.
IF you're in Windows, you can use GetAsyncKeyState
. (see: http://msdn.microsoft.com/en-us/library/ms646293(v=vs.85).aspx ). However, if you're not, this won't work (obviously) :)
If it is user-software, I'd go for the multi-threaded approach as mentioned by smichaud. If it is a tool (and performance isn't an issue), you could read from a file continuously and change that file from outside, as this the simplest implementation.
精彩评论