Click here to Skip to main content
15,884,836 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to take an input while an infinite loop is running in C++ .but i dont want ot halt my programme .what should i do.

What I have tried:

plzz help me out!!!!!! i dont want to use getch function . if it is possible then plzz tell me how can i solve this with kbhit function.
Posted
Updated 13-Jan-18 23:51pm
Comments
saide_a 23-Jan-18 2:44am    
As OriginalGriff said I think, the solution is use of threading (pthread).

1 solution

kbhit if not necessarily the way to go, for starters it doesn't work in all environments - kbhit | Microsoft Docs[^]:
Quote:
This API cannot be used in applications that execute in the Windows Runtime. For more information, see CRT functions not supported with /ZW.

Even if you can use it, it is depreciated, and you would need _kbhit | Microsoft Docs[^] instead.

You use it by calling it at intervals while you go round your loop, and if true, doing something (reading the character for example) which may cause you to exit the loop.

But it's a bad solution: using a sledgehammer to crack a nut.
What you should be doing is running your long running task in a separate thread so that your UI - whatever UI environment you are running under - does not become "blocked" by the loop.
Look under the documentation for your system for "threading" and see if you can find some examples - I'd point you at some, but I have no idea what environment you are trying to work in!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900