1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Turbo C++

Discussion in 'Software Development' started by motocross95, Dec 11, 2001.

Thread Status:
Not open for further replies.
Advertisement
  1. motocross95

    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.
     
  2. MacFromOK

    MacFromOK

    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();
    
     
  3. motocross95

    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
     
  4. MacFromOK

    MacFromOK

    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");
        }
    }
    
    
     
  5. motocross95

    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.
     
  6. MacFromOK

    MacFromOK

    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...
     
  7. Sponsor

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 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/61492

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice