Thread: using Arrow keys & random number generator

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    15

    using Arrow keys & random number generator

    I'm a beginner at C. I'd like to know how the arrow keys can be used in C. What type of input does the compiler receive when one of the arrow keys is pressed ? How can we use it in a program(like up means next entry,down means previous) ?

    I'd also like to know as to how a random number(within certain limits) can be generated in C ?

  2. #2
    Registered User krishnapollu's Avatar
    Join Date
    Dec 2010
    Location
    KERALA
    Posts
    29
    when u press an arrow key, it returns two values. a null character followed by an ascii value.
    the ascii values returned are as follows:

    right- 75
    left- 77
    up- 80
    down- 72

    u can generate random numbers using the functions random(),rand(), srand()...
    eg: random(9) generates a random number b/w 0 and 9

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    15
    but how to use it a program ? could you give an example ?

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by revanthb3000 View Post
    but how to use it a program ? could you give an example ?
    Sit down and put on your thinking cap.... How many things happen by random?

    Rolling dice,
    Shuffling cards,
    guessing games,
    and on and on...

    So... now you have a whole bunch of programs to write while exploring the possibilities of srand() and rand() ...

    Random() is not part of standard C, neither are the arrow keys.

  5. #5
    Registered User
    Join Date
    Jan 2011
    Posts
    15
    @CommonTater : I wasn't talking about the random function. I was asking about the arrow keys usage. How do you get the compiler to receive the arrow keys input ? What code/function do you have to enter in the program ?

  6. #6
    Registered User krishnapollu's Avatar
    Join Date
    Dec 2010
    Location
    KERALA
    Posts
    29
    Quote Originally Posted by revanthb3000 View Post
    I was asking about the arrow keys usage. How do you get the compiler to receive the arrow keys input ? What code/function do you have to enter in the program ?

    I think u want to know how the arrow keys can be sensed while running a program.
    here is a simple example.

    Code:
    #include <stdio.h>
    #include <conio.h>
     
    int main(void)
    {
      unsigned char key, key2;
     
      while( (key=getch()) != '\r' )
      {
        if (key == 0)
        {
          key2 = getch();
          switch(key2)
          {
            case 72:
              printf("up "); break;
            case 75:
              printf("left "); break;
            case 77:
              printf("right "); break;
            case 80:
              printf("down "); break;
            default: break;
          }
        }
      }
      return 0;
    }

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Except that conio is not part of standard C and is not supplied with all compilers.

    There are third party libraries for this, google is your friend.


    @krishnapollu ... which compiler are you using?

  8. #8
    Registered User
    Join Date
    Jan 2011
    Posts
    15
    Thats makes life a lot easier for me..Thanks a lot guys

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Random Number Generator - Error
    By Excelisus in forum C Programming
    Replies: 23
    Last Post: 09-05-2010, 11:07 PM
  2. How exactly does the random number generator work?
    By Finchie_88 in forum C++ Programming
    Replies: 6
    Last Post: 08-24-2007, 12:46 AM
  3. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 01:31 AM
  4. Testing Random Number Generator
    By Roaring_Tiger in forum C Programming
    Replies: 7
    Last Post: 08-12-2005, 12:48 AM
  5. Random number generator
    By Caze in forum C++ Programming
    Replies: 6
    Last Post: 12-03-2002, 08:10 AM