Posted: Tue Aug 21, 2007 2:55 pm Post subject: [snippet : C++] gathering input but not waiting for a return
I've used this many times - useful when you want to keep a program doing something but would like the user to be able to interact. The reason you can use just a normal getchar()/getch()/cin statement is because it will wait for the user to hit enter - halting until they do.
You will need to include conio.h for kbhit() to work. Basicly kbhit() returns a 0 or 1 depending if a key is hit. This way it never attempts to read the buffer unless a key has been hit. This example will loop until the user strikes enter.
This will loop until enter is hit, however it will echo out the keys being pressed (so it looks like a normal cin statement) - I recently used this one.
Practical uses? I've used this before in a bejeweled console clone - this would allow the game time to keep running while the user did not hit anything. Another thing is it's a pain to look at sometimes in a debugger... So if your trying to mess with people in crackme's - feel free.
Hope this helps some people
edit: i posted a function use for this on the forum here. _________________
0x7A 0x61 0x72 0x74
TEAM RESURRECTiON
Last edited by zart on Fri Aug 24, 2007 10:16 am; edited 1 time in total
That doesn't stop the problem; the loop is still halting the thread's execution until there's input (unless you make a new thread for it but you could've done that anyway..) _________________
That doesn't stop the problem; the loop is still halting the thread's execution until there's input (unless you make a new thread for it but you could've done that anyway..)
It does solve the problem... It will grab the keystoke entered (or keep looping if nothing has been hit) without pausing for a return carrage. _________________
oh! I thought you meant a return as in a function return.
Nope - sorry for being so confusing.
I mean this avoids having to hit the enter key for your program to continue doing things and read the input.
A normal cin statement - the user must hit enter before the program can read the buffer and continue the flow of the program. This eliminates the need for that. _________________
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You can download files in this forum