getch() and getche()

  • Thread starter James McLaughlin
  • Start date
J

James McLaughlin

Hi,

Does anyone know if it's possible to write either a getch()
(preferably Borland-style) or getche() function in C without it being
platform-dependent or otherwise non-standard?

If not, can someone suggest a good alternative for situations where
you require the user to "press any key to continue" or "press Enter to
continue" - with getch(), the mistakes that a user can make with
getchar() and the like just don't occur.

Thanks,

James McLaughlin
 
F

Flash Gordon

James said:
Hi,

Does anyone know if it's possible to write either a getch()
(preferably Borland-style) or getche() function in C without it being
platform-dependent or otherwise non-standard?

It isn't possible.
If not, can someone suggest a good alternative for situations where
you require the user to "press any key to continue" or "press Enter to
continue" - with getch(), the mistakes that a user can make with
getchar() and the like just don't occur.

Press Enter to continue is the best you can do in standard C. If you
want something better you will have to use implementation specific
routines. You could, of course, use a widely ported library such as
ncurses to handle it for you, but such libraries are off topic here.
 
M

Mark Odell

James said:
Hi,

Does anyone know if it's possible to write either a getch()
(preferably Borland-style) or getche() function in C without it being
platform-dependent or otherwise non-standard?

You cannot. The fact that those two functions are not part of C is a
hint.
If not, can someone suggest a good alternative for situations where
you require the user to "press any key to continue" or "press Enter to
continue" - with getch(), the mistakes that a user can make with
getchar() and the like just don't occur.

I would write a checkForChar() function for all platforms you wish to
support and then link against the correct version for a given platform.
 
M

Martin Ambuhl

James said:
Hi,

Does anyone know if it's possible to write either a getch()
(preferably Borland-style) or getche() function in C without it being
platform-dependent or otherwise non-standard?

Of course it's not possible. How could something that depends on the
characteristics of the OS and the hardware be independent of platform?
If not, can someone suggest a good alternative for situations where
you require the user to "press any key to continue" or "press Enter to
continue" - with getch(), the mistakes that a user can make with
getchar() and the like just don't occur.

Look at what you wrote: "press Enter to continue". What mistake that
the user can make with getchar() do you have in mind? It seems obvious
that mistakes with the (misleading and false) "press any key to
continue" style are much more likely.
 
J

Jack Klein

Hi,

Does anyone know if it's possible to write either a getch()
(preferably Borland-style) or getche() function in C without it being
platform-dependent or otherwise non-standard?

If not, can someone suggest a good alternative for situations where
you require the user to "press any key to continue" or "press Enter to
continue" - with getch(), the mistakes that a user can make with
getchar() and the like just don't occur.

Thanks,

Others have already given you good advice about the question you
asked. I'd like to give you some for the question you may well want
to ask next.

I would recommend never mixing input techniques in a program. Don't
mix fgets() or scanf() with getchar(). Don't mix any standard C
library input function with a non-standard function that bypasses the
C stream mechanism, be that getch(), getche(), bioskey(), or anything
else. Note that these functions do not read from stdin, they read
from a hardware keyboard, which is not the same thing.

If you do use some non standard keyboard input mechanism, use that
mechanism exclusively in your program. If necessary, write your own
string/line input mechanism using that function.
 
J

James McLaughlin

A belated thanks to you all for your help!

But, to Martin Ambuhl, considering getchar is a standard function, as
is getc, it didn't seem all that obvious that getch couldn't be
written without failing to be standard C.

James McLaughlin.
 
C

Chris Torek

... considering getchar is a standard function, as is getc, it
didn't seem all that obvious that getch couldn't be written without
failing to be standard C.

This is why it is a good idea to have a decent reference handy.
(The best reference(s) is/are of course a copy of the standard(s)
in question themselves, but Harbison & Steele is a good book to
use too, and there are others mentioned in the FAQ at c-faq.com.)

It is worth pointing out that writing Standard C (or Standard POSIX
or Standard FooCorp-Rev-1.3a-as-of-2:23-PM or whatever) is not
always useful or desirable. It is up to the programmer to decide
when and whether the benefits of sticking to any particular standards
are worth their costs. But in order to make such a decision (and
execute it), information *about* those standards is very important.

The newsgroup comp.lang.c already has a great deal of traffic, so
it makes sense to restrict it to just a few limited standards,
specifically those that specify ANSI and ISO C (C89, C90, C99,
and some of the various intermediate ones due to TCs and NAs).
 
S

santosh

James said:
A belated thanks to you all for your help!

But, to Martin Ambuhl, considering getchar is a standard function, as
is getc, it didn't seem all that obvious that getch couldn't be
written without failing to be standard C.

Functions like getch() and getche() haven't been incorporated into the
C standard because not all host systems provide ways to turn off input
buffering. Thus compilers include these as common extensions wherever
possible.
 
K

Kenneth Brody

santosh said:
Functions like getch() and getche() haven't been incorporated into the
C standard because not all host systems provide ways to turn off input
buffering. Thus compilers include these as common extensions wherever
possible.

Same for turning off echo. I've worked on systems where the terminal
runs in half-duplex mode, where the O/S doesn't echo what you type, and
the terminal itself displays the characters as it sends them. On such
systems, getch() can't work, as echo can't be turned off.

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:[email protected]>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top