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/
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>...
>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>
>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).
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
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,
> 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.
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.
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.