Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Platform independent getch()

92 views
Skip to first unread message

Giulio Agostini

unread,
Jan 5, 2001, 5:43:52 AM1/5/01
to
Hello there,

I'm looking for a platform independent (i.e. STL) way of implementing
the simple "Press a key to continue..." routine.

Under win32, I used to

#include <conio.h>
getch();

but this doesn't work under UNIX.
I tried

char ch;
cin.get(ch);

but this echoes the character on the screen. Is there a way to suppress
this echo?

Thanks a lot in advance.
Ciao, Giulio


Sent via Deja.com
http://www.deja.com/

Zull

unread,
Jan 5, 2001, 7:55:33 AM1/5/01
to
getch() is not standard ANSI C better use fgetchar()

example :


#include <stdio.h>

int main(void)
{
char ch;

/* prompt the user for input */
printf("Enter a character followed by <Enter>: ");

/* read the character from stdin */
ch = fgetchar();

/* display what was read */
printf("The character read is: '%c'\n", ch);
return 0;
}
Giulio Agostini napisał(a) w wiadomości: <9348h7$75q$1...@nnrp1.deja.com>...

Daniel Graf

unread,
Jan 5, 2001, 9:46:44 AM1/5/01
to
On Fri, 5 Jan 2001 13:55:33 +0100, "Zull" <z...@gss.abis.lodz.pl>
wrote:

>getch() is not standard ANSI C better use fgetchar()
>
>example :
>
>

<CODE CLIPPED>

I think Zull means fgetc rather than fgetchar. If you have <cstdio>
use it rather than <stdio.h>

Daniel Graf

unread,
Jan 5, 2001, 9:59:03 AM1/5/01
to
On Fri, 05 Jan 2001 10:43:52 GMT, Giulio Agostini
<giulio_...@my-deja.com> wrote:

>Hello there,
>
>I'm looking for a platform independent (i.e. STL) way of implementing
>the simple "Press a key to continue..." routine.
>
>Under win32, I used to
>
> #include <conio.h>
> getch();
>
>but this doesn't work under UNIX.
>I tried
>
> char ch;
> cin.get(ch);
>
>but this echoes the character on the screen. Is there a way to suppress
>this echo?


There is no standard way to emulate getch() (as I've seen it
implemented anyways).

The standard input stream can be "line buffered", and it usually is.
What that means is that getchar() and cin.get() won't return until a
newline has been encounter (the user presses <ENTER> or <RETURN>).

The echo is system dependent and there is no standard way to control
it.

On unix systems (that I know of) you can turn off both line buffering
and echo by using termios functions. (man termios). There is also a
getch() implemented by the curses library (man curses or man ncurses).

Giulio Agostini

unread,
Jan 5, 2001, 9:45:03 AM1/5/01
to

> getch() is not standard ANSI C better use fgetchar()

Thanks a lot!

By the way, I read some of the previous postings and discovered that
this is a FAQ (sorry about that).
Still, I'd be happy to know whether it is possible to suppress std::cin
echoing effect...

Cheers, Giulio

Giulio Agostini

unread,
Jan 5, 2001, 9:51:29 AM1/5/01
to

> getch() is not standard ANSI C better use fgetchar()

By the way, in previous postings I have read that it's better not to
mix STL <iostream> with C-style or OS-API I/O libraries.
Is there any side-effect using fgetchar() together with <iostream>?

Cheers,

Giulio Agostini

unread,
Jan 5, 2001, 9:59:05 AM1/5/01
to
Aaaargh, I'm sorry, it's me again.

> printf("Enter a character followed by <Enter>: ");

This was not exactly what I had in mind, the user should just hit a
key, without <Enter>ing it.

Thanks, anyway.

Ron Natalie

unread,
Jan 5, 2001, 10:41:27 AM1/5/01
to

Giulio Agostini wrote:
>
> > getch() is not standard ANSI C better use fgetchar()
>
> Thanks a lot!
>
> By the way, I read some of the previous postings and discovered that
> this is a FAQ (sorry about that).
> Still, I'd be happy to know whether it is possible to suppress std::cin
> echoing effect...
>

There IS NO standard way C or C++ to do literally what getch does.

The echoing (and the waiting for the end of line) from keyboards
is an operating specific issue which works differently accross C++
implementations.

quu...@newsguy.com

unread,
Jan 5, 2001, 10:29:57 AM1/5/01
to

Are you saying you want some kind of KeyDown() function? If so, then
there's probably no platform independant way of doing that since you'd
have to do some interrupt-calling from the keyboard...ioctl() or
something similar.


0 new messages