Qbasicnews.com

Full Version: REDIM equivalent in C?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
I think I get it. Smile
Okay, next question - is there a function that is the equivalent of INKEY$?
Not exactly, but you can do some workarounds with this:

Code:
#include <conio.h>
#include <stdio.h>

int main(void)
{
   char c;

   printf("Press 's' to exit \n");

   do
   {

      /* If there is a character in the keyboard buffer... */
      if (kbhit())
      {
         c = getch();  /* get it */
      }
   } while (c!='s');
}

The above code works in Borland C, and I am not sure about it is portable or not (try it in your compiler, it may work). That "kbhit" stuff sounds quite nasty, so...

...for real usability and portability, you better learn to work with a keyboard handler, for example allegro's Wink

Allegro's work like this:

Code:
#include <allegro.h>
#include <stdio.h>

int main(void)
{
   allegro_init();
   install_keyboard();  /* Installs the keyboard handler */

   printf("Press s to exit\n");

   while(!key[KEY_S]);

   remove_keyboard();
   allegro_exit();
}
Erm...don't you need END_OF_MAIN() at the end of an allegro app?
Also, I'm using mingw32...so no conio.h for me. Sad
Yeah, you need that END_OF_MAIN; I was testing my code in DJGPP (like your compiler but for MSDOS) and you don't need it Tongue

Anyways, I don't know another replacement. Use Allegro's Smile
Btw, what's with that WHILE statement? No following statement or code block...huh?
Look at this:

The while statement is

Code:
while (<condition>)
   instruction;

Instruction can be a single instruction:

Code:
while(!key[KEY_S])
   rectfill(screen, 0, 0, SCREEN_H, SCREEN_W, makecol(rand()&255,rand()&255,rand()&255));

or an instruction block:

Code:
while(!key[KEY_S])
{
   // Stuff here
}

I just used the simplest instruction: ";" which does nothing:

Code:
while(!key[KEY_S])
   ;

Note that while(<condition>) is not followed by a colon but by an instruction. My instruction is null, so the following colon is the separator after a null instruction. I am used to write it in a single line:

Code:
while(!key[KEY_S]);

that while just waits until 's' is pressed, does nothing in the meantime, just wait. In QB:

Code:
WHILE (NOT key(KEY_S)):WEND

What I like most about C is the flexibility in its syntax.
Ah! I see. What I like about C, also, is that you can do a lot on one line. :wink:
Pages: 1 2