Thread: any alternative for getch() ?

  1. #1
    Registered User kiss_psycho's Avatar
    Join Date
    Feb 2003
    Posts
    49

    any alternative for getch() ?

    Ok, then i am not supposed to use getch();. But is there any analogous ANSI version that waits for a character input and assigns that character to a variable without waiting for the return key to be pressed and/or doesn't display the character typed?

    All versions of scanning input I know except getch() and some analogous versions defined in conio.h wait for the carriage return to be hit before accepting the input.

    Help!
    Definition of Programmer : A red-eyed mumbling mammal capable of conversing with inanimate objects.

    Happy Hunting...
    The Root

  2. #2
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    You can use virtual keys if its just a character. Say you want the program to quit if the user presses "Q":

    NOTE: Its been a while since i used VK this code might not build on one shot, i think the header <windows>. I doubt the VK_Q line is 100% accurate but i'm rush-writing this.

    Code:
    if (userInput == VK_Q)
    {
        ..................
        ................
        .....
    }

  3. #3
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Sorry. Its not possible in a portable manner without some form of conditional compilation using the correct API functions for each different OS to access unbuffered keyboard input.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  4. #4
    Registered User
    Join Date
    Jan 2003
    Posts
    78
    This one have been discussed over and over many, many times. The point is generally "can one really expect an end user to use an app that requires the return key to be pressed after each input?". The honesy answer is generally, no. Typically end users will not stand for it. But, if you really intend to distribute an app, and you really want people to use it, you're almost certianly going to give it a GUI. In order to do that you have to use a whole bunch of non-ansi standard code and libs.

  5. #5
    Mmm. Purple.
    Join Date
    May 2002
    Posts
    154
    getche() ?

  6. #6
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    getche() is the same as getch() except it is echoed to the screen also. Both functions are non standard. There is no way of doing this in standard c++ without using an api.
    When compilers offer a getch() and getche() this only means that the compiler writers have already done the job for you. On a windows system getch() will undoubtedly call GetKeyboardState() or some similar windows api function to do the real work.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  7. #7
    Registered User newbie_grg's Avatar
    Join Date
    Jul 2002
    Posts
    77

    ok..

    try using gets() , getline() , read() or any relevent function.. guess it might work ...
    "If knowledge can create problems, it is not through ignorance that we can solve them. "
    -Isaac Asimov(1920-1992)

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    Re: ok..

    Originally posted by newbie_grg
    try using gets() , getline() , read() or any relevent function.. guess it might work ...
    Then you've guessed wrong. And don't recommend gets() again, no matter what

    >>This one have been discussed over and over many, many times.
    Echo that.

    The question is answered by Stoned_Coder, lets end this pointless conversation please.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ASP.NET alternative to gallery.menalto.com ???
    By gicio in forum C# Programming
    Replies: 0
    Last Post: 05-15-2005, 11:31 AM
  2. square root alternative
    By FH|RogueHunter in forum C++ Programming
    Replies: 1
    Last Post: 03-28-2005, 10:17 PM
  3. Alternative forms of printf.
    By OOPboredom in forum C Programming
    Replies: 5
    Last Post: 05-07-2004, 01:21 PM
  4. Alternative win32 compiler? And linking asm libs?
    By JMB in forum C++ Programming
    Replies: 4
    Last Post: 10-11-2002, 01:22 PM
  5. Alternative to fstream
    By golfinguy4 in forum Windows Programming
    Replies: 3
    Last Post: 07-03-2002, 01:51 AM