Advertisement

Simple Console IO Question

Started by February 14, 2006 01:08 PM
1 comment, last by jkleinecke 18 years, 9 months ago
I'm programming a text-based game, and am having trouble with the "Press enter to continue" scenario. Occasionally, the user will press enter once and it will act as though it was pressed several times, overflowing into the other inputs. How can I fix this? Right now, I'm using this little routine: getchar();
You have to use some kind of timer that disables the enter key for a short time, long enough for the user to have time to let go of the key and short enough for it to never show, sort of like this

delay+=t;
if (delay>1) delay=1;

if ((g_keys->keyDown [VK_RETURN]) && (delay>0.2))
{

--do whatever--

delay=0;
}


t is the delta time for each frame(or loop in your case), t=1 is a second.

alternatively you can check if the key is pressed down and then released instead of just pressed down.
Advertisement
I know that if you are using scanf() anywhere in your program, getchar() will register the enter key that signifies the end of input. You can get around this by using a dummy input right after your intended input.

scanf(szText, "%c") ;
scanf(szDummy, "%c") ;
-----------------------------------Indium Studios, Inc.

This topic is closed to new replies.

Advertisement