Two Easy Programming Questions

Started by
8 comments, last by wardekar 23 years, 4 months ago
I''m trying to think of the old DOS function that returns a boolean that tells you if ANY key is being pressed. And what header it''s in. And I forget the old DOS function to convert an integer back to a character, like int blah=getch();, how do you then go back and display blah in it''s character form? Thanks.
~WarDekar
Advertisement
I think the first one you''re thinking of his kbhit(). And I think it''s declared in conio.h. Not sure about the second one though.


"We are the music makers, and we are the dreamers of the dreams."
- Willy Wonka
The following code will read a character if one is pressed, and output it to the screen, otherwise it will do nothing. This is not a realistic use of the functions, but you''ll get the idea .
  #include <conio.h>#include <stdio.h>int main(void) {  while(1) {    if(kbhit()) {      printf("%c\n",(unsigned char) getch());    } else {      // No key is held down...    }  }}  



http://www.gdarchive.net/druidgames/
Yup that''s it, thanks.

~WarDekar
Ahh, he posted while I was typing =P.


http://www.gdarchive.net/druidgames/
That method doesn''t work. What I''m trying to do is if a key is being pressed, add that character to a string.

~WarDekar
That method doesn''t work. What I''m trying to do is if a key is being pressed, add that character to a string. Also, the character must be a number or a period.

~WarDekar
I was just demonstrating the uses of the different functions . I, of course, expected you to adapt it.


http://www.gdarchive.net/druidgames/
quote:What I'm trying to do is if a key is being pressed, add that character to a string. Also, the character must be a number or a period.


      #include < string.h >char string[80];..................while(TRUE) {   if(kbhit()) {      if(strlen(string)+1 > 79) {         break;      }      char ch = getch();      if(ch == '\n') {         sprintf(string, "%s\0", string); // Not sure if needed         break;      }      if(ch >= 48 && ch <= 57) {         sprintf(string, "%s%d", string, ch);      } else {      if(ch == 46) {         sprintf(string, "%s.", string);      } else {         sprintf(string, "%s%c", ch);      }}   }}..................  


Hope I didn't forget anything!

Edited by - Zipster on November 26, 2000 12:41:29 AM
Extra Note If You Happen To Be Writing a DOS Game:

Best way to get input from keyboard in DOS when doing stuff like games (where normal kbhit and getch just dont give ample performance), latch onto the keyboard interrupt. Too tired right now to remember the exact code, but if u do need it, i would be happy to post it. You can also find that technique in "Tricks of the Game Programming Gurus" by Andre LaMothe (sp) and company
BetaShare - Run Your Beta Right!

This topic is closed to new replies.

Advertisement