delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2001/10/19/12:49:35

From: "A. Sinan Unur" <asu1 AT cornell DOT edu>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: getkey()
Date: 19 Oct 2001 16:36:08 GMT
Organization: Cornell University
Lines: 79
Sender: asu1 AT cornell DOT invalid (on 128.253.251.163)
Message-ID: <Xns913F803093AC5ASINANUNUR@132.236.56.8>
References: <9qpj2s$96v$1 AT cyan DOT nl DOT gxn DOT net>
NNTP-Posting-Host: 128.253.251.163
X-Trace: news01.cit.cornell.edu 1003509368 8494 128.253.251.163 (19 Oct 2001 16:36:08 GMT)
X-Complaints-To: usenet AT news01 DOT cit DOT cornell DOT edu
NNTP-Posting-Date: 19 Oct 2001 16:36:08 GMT
User-Agent: Xnews/4.06.22
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

"Bart van den Burg" <bart AT bart99 DOT tmfweb DOT nl> wrote in 
news:9qpj2s$96v$1 AT cyan DOT nl DOT gxn DOT net:

> I've got a problem with this function:

you have a few.
 
> --------------------------------
> void playGame() {

good idea to explicitly decalre playGame as a no-arg function by using:

void playGame(void)

>  char x;
>  char y;

see the docs: info libc alpha getkey. getkey returns an int.

>  x = getkey();
>  char buffer[2];
>  sprintf(buffer, "%d", char(x));

now, seriously, what does char(x) mean? what is regel? have you tried 
compiling this? here's what I get:

C:\var>gcc -c key.c -Wall
key.c: In function `playGame':
key.c:10: parse error before `char'
key.c:11: `buffer' undeclared (first use in this function)
key.c:11: (Each undeclared identifier is reported only once
key.c:11: for each function it appears in.)
key.c:11: parse error before `char'
key.c:12: warning: implicit declaration of function `regel'
key.c:14: parse error before `char'

it is a good idea to post code that compiles so others can test it.

> when I press, for example, "1", it says "49" instead of "1".
> How can I fix this without changing the contents of the regel() function?

Where did regel come from?

if you want to print the character that corresponds to a given integer, 
then you should tell sprintf that is what you want.

>  sprintf(buffer, "%d", char(x));

I still don't know what you mean by char(x), but the format character 
should be %c, not %d. When dealing with computers, it helps to remember 
that the computer does exactly what you ask it.

#include <stdio.h>

#include <pc.h>
#include <keys.h>

void playGame(void) {
	int x;

	x = getkey();
	printf("Key pressed: %c\n", x & 0xff);

	return;
}

int main(void)
{
	playGame();

	return 0;
}



-- 
--------------------------------
A. Sinan Unur
http://www.unur.com/

- Raw text -


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