getch();?

Mar 3, 2009 at 2:35pm
can anyone tell me what getch(); does? and do i need to include another header aside from <iostream> to use it?
Mar 3, 2009 at 3:11pm
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 Mar 3, 2009 at 3:14pm
Apr 19, 2009 at 11:06pm
What could be used as a supplement for getch() then?
Apr 20, 2009 at 3:51am
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?
Apr 20, 2009 at 11:32am
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
Apr 22, 2009 at 5:26pm
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.
Apr 22, 2009 at 10:33pm
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.
Apr 23, 2009 at 1:47am
Excellent! This takes care of the 100% cpu on wait of the clock function. cool and thanks.
Apr 23, 2009 at 1:54am
Just it is (obviously) windows only.
Apr 23, 2009 at 10:20am
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.
Apr 23, 2009 at 10:23am
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.