getch();?

can anyone tell me what getch(); does? and do i need to include another header aside from <iostream> to use it?
getch() should be in conio.h and it pauses the console until a key is hit and it returns a char for the value of the key pressed. Some special keys (as arrow keys) return two values

http://en.wikipedia.org/wiki/Conio

--Functions from conio.h should be avoided--
Last edited on
What could be used as a supplement for getch() then?
I use _getch(); in many of my beginner proggys just because i need the console held open on termination, but I am curious, is there a preferred way to just do a clrscrn and what the _getch accomplishes without having to call it explicitly ? What I mean to say is this: When I call _getch, yes I need the console to stay open. Can't I just do a 'wait' function of some sort on a timer, then term minus user input within defined parameters of the wait function?
There are many ways of keeping the console window open: http://www.cplusplus.com/forum/articles/7312/
C++ has not a real wait function but you can build it using the C time library: http://www.cplusplus.com/reference/clibrary/ctime/clock/ (that example uses 100% CPU on 'wait' function)
To have the best functions for console stuff you might use curses or Windows API.
The curses library also have a getch which works the same way as the getch from conio
Thanks! That example is right on the nailhead for what I am looking for, as I don't really see the point of using _getch() function when having to manually close the console seems redundant avtivity in some cases, where user input is requested, but not known to exist as an predictable occurrence. At any rate, this seems interesting, and seems like a good base to allow an activity to poll for user input (given some tweakage, of course). Thanks again for your speedy reply.
or you can use
Sleep(
__in DWORD dwMilliseconds
);

from
#include "windows.h"

you call it like this

Sleep(1000);// it waits for 1 sec and then contiunes the program.
Excellent! This takes care of the 100% cpu on wait of the clock function. cool and thanks.
Just it is (obviously) windows only.
I know how to do it only in windows. I programmed only in windows so far, and I don't think I'll do anyother kind of programming.

The Sleep function allows other programs to use the processor while your program waits. It doesn't use the processor for your program. This is why you don't have 100% CPU, and if you check it will be 0% CPU for your application while it waits.

Sorry for my english.
And I checked a site... It seems there is something similar in linux too

http://linux.die.net/man/3/sleep

#include <unistd.h>
unsigned int sleep(unsigned int seconds);

Description
sleep() makes the current process sleep until seconds seconds have elapsed or a signal arrives which is not ignored.
Return Value
Zero if the requested time has elapsed, or the number of seconds left to sleep.
Topic archived. No new replies allowed.