how to use arrows as input?

This is a discussion on how to use arrows as input? within the C Programming forums, part of the General Programming Boards category; How could I ask the user to give me an answer such as <- (left arrow)? Untill now I have ...

  1. #1
    csd@auth
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    71

    Unhappy how to use arrows as input?

    How could I ask the user to give me an answer such as <- (left arrow)?
    Untill now I have seen programs that user is expected to type a char,string or number.

    I just need to make a "simple" program that shows

    *
    *
    *
    *




    depending on what arrow user inputs.

    I 'somehow' did it with
    w
    a s d

    Code:
    #include<stdio.h>
    #include<conio.h>
    int main()
    {
     char ch;
    
     ch=getch();
     while(1)
     {
     if(ch=='a') printf("\n\n*");
     if(ch=='d') printf("\n\n\t\t*");       
     if(ch=='w') printf("\t*");
     if(ch=='s') printf("\n\n\n\t*");
     ch=getch();        
    }      
    getchar();
    return 0;
    }
    but I need a function to CLEAR output after every input.
    fflush(stdout) didn't work while clrscr (clearscreen function) is unknow
    in my compiler. I use dev-c++ 4.992.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    regarding the clear function you can use system("cls") but that will only work on windows.
    search around if you need another option

  3. #3
    csd@auth
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    71

    An answer at last!
    Thanks nadroj. system("cls"); is much better than fflush(stdout); and it works if i include stdlib.h.

    ANY SUGGESTION ABOUT ARROWS?

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You have conio.h on your version. Check it out. Mine has clrscr(), and it works fine in Windows 2000 and XP's console window.

    Another approach would be to use your conio.h file, or a Window API, to tell you where the CP (current or cursor position) was as the * is being printed. Then just have your program putchar or printf, a blank space (ascii 32), right over the *.

    Clearing the screen multiple times quickly, will cause your screen to flicker annoyingly. I recommend the "another" approach.

    Adak

  5. #5
    csd@auth
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    71
    Well. I fixed this and it works!!
    My problem now is how to use arrows instead of w,a,s,d?

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    int main()
    {
     char ch;
    
     ch=getch();
     while(1)
     {
     if(ch=='a'){ printf("\n\n\n\n\n\n\n\n\n\t*");}
     if(ch=='d'){ printf("\n\n\n\n\n\n\n\n\n\t\t\t\t\t\t\t*");}       
     if(ch=='w'){ printf("\t\t\t\t*");}
     if(ch=='s'){ printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\t\t\t*");}
    
     ch=getch();        
     system("cls");
    }      
    getchar();
    return 0;
    }
    Adak I searched my conio.h file and it doesn't have clrscr() function . I suppose that your second way is much better but I can't understand it.
    Last edited by kantze; 10-28-2006 at 09:21 AM.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Not every language version will have clrscr(). In any case, using it repeatedly is not what you want - way to much flickering.

    From the FAQ:
    Q: How do I read the arrow keys? What about function keys?



    --------------------------------------------------------------------------------

    A: Terminfo, some versions of termcap, and some versions of curses have support for these non-ASCII keys. Typically, a special key sends a multicharacter sequence (usually beginning with ESC, '\033'); parsing these can be tricky. (curses will do the parsing for you, if you call keypad first.)

    Under MS-DOS, if you receive a character with value 0 (not '0'!) while reading the keyboard, it's a flag indicating that the next character read will be a code indicating a special key. See any DOS programming guide for lists of keyboard scan codes. (Very briefly: the up, left, right, and down arrow keys are 72, 75, 77, and 80, and the function keys are 59 through 68.)

    References: PCS Sec. 5.1.4 pp. 56-7


    If you need a sample of code to do this, let me know. This would be old DOS based code (which should work in a Windows text window, but no guarantee's). In a bit I'll be taking off for an hour or so for breakfast and etc.

    Adak
    Last edited by Adak; 10-28-2006 at 08:01 AM.

  7. #7
    csd@auth
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    71
    Well could you give me the link of the FAQ?

    Originally Posted by Adak:
    If you need a sample of code to do this, let me know. In a bit I'll be taking off for an hour or so for breakfast and etc.
    An example would surely help me!
    Take your time and answer whenever you can!

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    FAQ is: http://c-faq.com/index.html

    Give me 5 minutes, and I'll have the sample up.

    OK, maybe 10 - no coffee yet!

    This is old stuff, here. Relies on a header called "bios.h". Have no idea if ANSI ever heard of this one!

    And it's more advanced than I remembered it to be:

    bioskey() accesses the BIOS keyboard services. It has 3 parts:

    0) Reads the next char in the kb buffer, and removes them.
    1) Checks the kb buffer for waiting char's. Doesn't remove them, but tells you what they are.
    2) Checks and returns status of various shift, control, alt, caps lock, keys. NOT the arrow keys, however.

    Maybe it's just me but I'm not getting it to work with the arrow keys on this WindowsXP rig.

    Here's an old example of a #0 usage:

    Code:
    #include <stdio.h>
    #include <bios.h>
    
    #define KBRD_GOOD 1
    #define KBRD_READ 0
    
    int main()  {
    
      char c = 'a';
    
      while(c != 'q')  {
         if(bioskey(KBRD_GOOD))  {
           c = bioskey(KBRD_READ) & 0xff;  /* reads key and mask out the high byte */
           printf("\nThat was a: %c", c);
         }
      }
      return 0;
    }
    The above works on my WindowsXP rig. I believe if one bioskey service works, they'll all work, but not without some breakfast and coffee!

    If you google for keyboard scan codes, or keyboard trapping, you'll find a lot of info.

    It's a start, but not quite what you wanted.

    Adak
    Last edited by Adak; 10-28-2006 at 09:27 AM.

  9. #9
    csd@auth
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    71
    OK. Thanks for the help and the time you spent.
    I'll check it out.
    The link was really helpful.
    I 'fixed' even more my code(just for fun)
    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    #include<windows.h>
    int main()
    { HANDLE h = GetStdHandle ( STD_OUTPUT_HANDLE );
      WORD wOldColorAttrs;
      CONSOLE_SCREEN_BUFFER_INFO csbiInfo; 
      
      /*
       * First save the current color information
       */
                                GetConsoleScreenBufferInfo(h, &csbiInfo);
                                wOldColorAttrs = csbiInfo.wAttributes; 
                                SetConsoleTextAttribute ( h, FOREGROUND_GREEN | FOREGROUND_INTENSITY );
     char ch;
    
     ch=getch();
     while(1)
     {
     if(ch=='a'){
                  printf("\n\n\n\n\n\n\n\n\n\tTHANKS");
           SetConsoleTextAttribute ( h, FOREGROUND_BLUE | FOREGROUND_INTENSITY );}
     else if(ch=='d')
     {
           printf("\n\n\n\n\n\n\n\n\n\t\t\t\t\t\t\tFOR");
           SetConsoleTextAttribute ( h, FOREGROUND_RED | FOREGROUND_INTENSITY );}       
     else if(ch=='w')
     {
           printf("\t\t\t\tTHE");
           SetConsoleTextAttribute ( h, FOREGROUND_GREEN | FOREGROUND_INTENSITY );}
     else if(ch=='s'){
           printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\t\t\t HELP!!!");
           SetConsoleTextAttribute ( h, FOREGROUND_RED | FOREGROUND_BLUE );}
     
     ch=getch();        
     if(ch=='a' || ch=='d' || ch=='w' || ch=='s')
     system("cls");
    }              
                                       SetConsoleTextAttribute ( h, wOldColorAttrs);   
    getchar();
    return 0;
    }
    Copy paste it and try pressing a,d,w,s.....
    Last edited by kantze; 10-28-2006 at 09:26 AM.

  10. #10
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Quote Originally Posted by Adak
    You have conio.h on your version. Check it out. Mine has clrscr(), and it works fine in Windows 2000 and XP's console window.

    Another approach would be to use your conio.h file, or a Window API, to tell you where the CP (current or cursor position) was as the * is being printed. Then just have your program putchar or printf, a blank space (ascii 32), right over the *.

    Clearing the screen multiple times quickly, will cause your screen to flicker annoyingly. I recommend the "another" approach.

    Adak
    Conio.h is evil...
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  11. #11
    csd@auth
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    71

    Angry

    What do you mean maxorator?
    Make a complete point and don't screw my thread

  12. #12
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    Quote Originally Posted by kantze
    What do you mean maxorator?
    Make a complete point and don't screw my thread
    It isn't portable, it isn't standart.

  13. #13
    csd@auth
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    71
    Ok. Guys my problem is so SOLVED now.
    Special thanks to citizen.

  14. #14
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    There's an FAQ on the subject on faq.cprogramming.com: http://faq.cprogramming.com/cgi-bin/...&id=1043284392
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. input redirection
    By sashaKap in forum C Programming
    Replies: 6
    Last Post: 06-25-2009, 01:59 AM
  2. Input statement problem
    By une in forum C Programming
    Replies: 3
    Last Post: 05-29-2007, 11:16 PM
  3. For loop problems, input please.
    By xIcyx in forum C Programming
    Replies: 2
    Last Post: 04-22-2007, 03:54 AM
  4. I would love some input on my BST tree.
    By StevenGarcia in forum C++ Programming
    Replies: 4
    Last Post: 01-15-2007, 12:22 AM
  5. Simple Console Input for Beginners
    By jlou in forum C++ Programming
    Replies: 0
    Last Post: 06-21-2005, 01:50 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21