Thread: How can I prevent the user from typing anything besides Return?

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    158

    How can I prevent the user from typing anything besides Return?

    I want to have a simples function where I print on screen with printf() lots of text and then do not allow the user to type anything but the return key. When the user presses the return key, the function exits (automatically), but before that, I don't anything to be on screen if the user presses any keys...

    How can I accomplish that?

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Why would you want to do that?

    If your compiler supports the non-standard function getch():
    Code:
    #include <conio.h>
    
    while(getch() != '\n');
    That might work. You may have to flush the input buffer with while(getchar()...) after using getch().
    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.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    158
    nope, i'm using gcc, no conio available...

    I think I could use curses/ncurses but I read that to run my app on another computer, that computer needs to have curses/ncurses library, my question is, isn't that only necessarya if compiling on another computer? Meaning, if I compile on my machine using those libraries and then share the executable file with someone that doesn't have them, will he be able to run the app without any problems?

  4. #4
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    You need to statically link to the libraries. Then there shouldn't be a problem.
    Kurt

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    If you're planning to use curses to do what you want (other that the static linking issue) look up "raw mode"

  6. #6
    Registered User
    Join Date
    Mar 2006
    Posts
    158
    Quote Originally Posted by grumpy
    If you're planning to use curses to do what you want (other that the static linking issue) look up "raw mode"

    could you be more specific? I wasn't able to understand what you meant...

  7. #7
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Quote Originally Posted by Nazgulled
    I want to have a simples function where I print on screen with printf() lots of text and then do not allow the user to type anything but the return key. When the user presses the return key, the function exits (automatically), but before that, I don't anything to be on screen if the user presses any keys...

    How can I accomplish that?
    As you can see from the posts, unless your compiler has some non-standard input function this is not a simple problem. Is this really something you must have? Is the solution beyond your current ability?
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The only true way to prevent them from typing it is to remove all other keys on the keyboard. Well maybe you could have a specially designed keyboard which only has one key. Or perhaps you could just glue tacks to all the other keys; but you'd still have some people that would press other keys anyway, regardless of what pain they knew they'd receive. So really, it's best to just remove all the other keys. That way they won't be tempted by the tacks, and you won't have to to through the messy costs involved with manufacturing your own keyboard.


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    Quote Originally Posted by Nazgulled
    nope, i'm using gcc, no conio available...

    I think I could use curses/ncurses but I read that to run my app on another computer, that computer needs to have curses/ncurses library, my question is, isn't that only necessarya if compiling on another computer? Meaning, if I compile on my machine using those libraries and then share the executable file with someone that doesn't have them, will he be able to run the app without any problems?
    Yes that's just for compiling on another computer and you could give someone your executable and he should be able to run it but remember that ncurses is for unix systems so it won't run on Windows or a system with different endianness.

  10. #10
    Just kidding.... fnoyan's Avatar
    Join Date
    Jun 2003
    Location
    Still in the egg
    Posts
    275
    There is a getch() wrapper for gcc.

    http://faq.cprogramming.com/cgi-bin/...&id=1043284385

    It makes some trick with termio.
    See the link above. Thanks Salem to suggest me this link.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. SSH Hacker Activity!! AAHHH!!
    By Kleid-0 in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 03-06-2005, 03:53 PM
  4. problem with open gl engine.
    By gell10 in forum Game Programming
    Replies: 1
    Last Post: 08-21-2003, 04:10 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM