Turbo C++

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

motocross95

Thread Starter
Joined
Oct 30, 2001
Messages
110
I am using Turbo C++ and I am making a simple program that displays circles and random X and Y coordinates and random colors. One problem, I'm not sure how to perform a loop in Turbo C++ until a key is pressed. I tried something like

do
{
//Code goes here
} while (getch() != "");

As you can guess it does not work, I get an error. I also just made it....while (getch()); But that just makes it pause after every loop until you press a key. Any help would be greatly appreciated thank you.
 
Joined
Mar 18, 2001
Messages
1,741
Here ya go. :)
Code:
while(!kbhit())  // Loop until user hits a key.
  { // Code goes here.
  }
if(getch() == 0) // Dispose of kbhit character if not needed.
   getch();
 

motocross95

Thread Starter
Joined
Oct 30, 2001
Messages
110
Thank you MacFromOK, it worked without any errors. Just one more question now. I'm trying to gather enough knowledge to eventually try making PONG. So eventually I'm gonna have problems with collission detection if anyone cares to elaborate on that now, but that question now is.....After it exits the loop how do I tell what key you pressed? as in LEFT, RIGHT, DOWN, UP, or Esc keys? I know escape is Chr$(27) in qbasic. Because I need to run a check and perform the correct operations depending on it. Thank you in advance
 
Joined
Mar 18, 2001
Messages
1,741
Here's a little prog I wrote a long time ago. It should
give you an idea how to check for the scan code, as
well as being a handy tool to find them. :)

Code:
#include &ltconio.h>
#include &ltstdio.h>

void main()
{
  int keyhit = 0;

  printf("\nKEYTEST by Mac. Displays scan code of key pressed. ESC to exit.\n\n");
  while(keyhit != 27)
    { keyhit = getch();
      if(keyhit > 0)
        printf("%c scan code = %d", keyhit, keyhit);
      if(keyhit == 8)
        printf(" (Backspace)");
      if(keyhit == 9)
        printf(" (Tab)");
      if(keyhit == 13)
        printf(" (Enter)");
      if(keyhit == 27)
        printf(" (Esc)");
      if(keyhit == 32)
        printf(" (Spacebar)");
      if(keyhit == 73)
        printf(" (Page Up)");
      if(keyhit == 81)
        printf(" (Page Down)");
      if(keyhit == 71)
        printf(" (Home)");
      if(keyhit == 79)
        printf(" (End)");
      if(keyhit == 82)
        printf(" (Insert)");
      if(keyhit == 83)
        printf(" (Delete)");
      if(keyhit > 0)
        printf("\n");
    }
}
 

motocross95

Thread Starter
Joined
Oct 30, 2001
Messages
110
That is a nice program thank you, it will be very helpful. I have a question. Run that program and tell me what it says when you type a capital S. notice it is the same as the delete key? I find that weird. It happens with other buttons as well, Insert, Home, Page Up, Delete, End, Page Down. Have any ideas as to why it does that? Thank you again for that nifty little program.
 
Joined
Mar 18, 2001
Messages
1,741
You are correct. That's why I put the "Insert",
"Delete", etc. in parenthesis. They are the
same as some capital letters. I don't know
why, but they are.

Basically, this just means that the same
getch() procedure will catch either keyhit.

Cheers, Mac :)

[EDIT]
The left/right and up/down arrow buttons
also share scan codes with capital letters...
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Latest posts

Staff online

Top