Thursday, July 30, 2009

How do you cin/getline while a c++ program is still running and displaying text?

I am writing a console app in dev c++ in which a timer runs down to zero. While the timer is running down, the user has to enter in a password. I have the program working except for getting the timer to run down while the user has a chance to cin, because it stops program flow until data is inputted. Here's a basic version of the countdown code:





for(int b=60;b%26gt;0;b--)


{


system("cls");


cout%26lt;%26lt;b%26lt;%26lt;endl;


cout%26lt;%26lt;"%26gt; ";


Sleep(60000);


}





Somewhere in that code I want the user to be able to stop the loop and enter in the password, but I do not want the loop to stop unless the user begins typing.

How do you cin/getline while a c++ program is still running and displaying text?
If you want to use C++ style input ( via cin ) and still have a time going down, you will have to look into thread programming.





Otherwise, abandoning the proper C++ way of doing things, you will have to resort to researching low level C calls that allow you to check if there is a character available via stdin.





I would recommend the threaded design, even though its more tricky to implement.


No comments:

Post a Comment