delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/09/07/23:48:23

From: "Jack Klein" <jackklein AT nospam DOT worldnet DOT att DOT net>
Newsgroups: rec.games.programmer,alt.msdos.programmer,comp.os.msdos.djgpp
Subject: Re: Text Mode Program: Help Please..
Date: 8 Sep 1997 01:18:28 GMT
Organization: AT&T WorldNet Services
Lines: 82
Message-ID: <5uvjp4$rdf@bgtnsc03.worldnet.att.net>
References: <5uv4i5$t3u AT bgtnsc02 DOT worldnet DOT att DOT net>
NNTP-Posting-Host: 207.146.175.39
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019