Mail Archives: djgpp/1997/09/07/23:48:23
Jay Slanker <slanker505 AT usa DOT net> wrote in article
<5uv4i5$t3u AT bgtnsc02 DOT worldnet DOT att DOT net>...
> I am just trying to make a simple text mode, move around demo.
Having a
> Zero move around the screen when you press 8(up), 6(right),
4(left), and
> 5(down). It compiles ok, but then when run, if you press any
of the
> above keys it repeptitively prints the zero key until you end
it(press
> escape). Please if anyone see's the problem in the following
code,
> e-mail me at: slanker505 AT usa DOT net. Thanks in advance. Reply
by e-mail
> please..
>
Hi Jay,
I rewrote part of your original code because there were so many
tabs in it that it is hard to read in my reader. The important
part is here:
do {
if (kbhit())
control = getch();
if (control == '8')
{
/* do something */
}
if (control == '6')
{
/* do something */
}
/* more compare and do something's */
} while (control != 27);
Here is your problem:
Because you did not use braces, the only statement that is
skipped if kbhit() is FALSE is "control = getch();". If control
was already set from a key and there is no new key, the if
statement which matches the last key hit will repeat every time
through the loop.
If you write the code like this:
do {
if (kbhit())
{
control = getch();
if (control == '8')
{
/* do something */
}
if (control == '6')
{
/* do something */
}
/* more compare and do something's */
}
} while (control != 27);
then the action is only taken one time when you press a key.
--
Jack Klein
All views expressed in this message are mine,
and not necessarily that of my company or any
of our clients.
Remove nospam from address to reply.
- Raw text -