Click to See Complete Forum and Search --> : g++ conio.h & getch() HELP!!!!!


gedger
10-16-2002, 10:13 AM
Hi,

I am completeing a course in OOB programming and have just installed Suse Linux.

I need to use getch() for a press any key function but as I have found out it doesn't work in g++.

Could someone give me an idea of what I can use instead.

many Thanks

Dodge

TheLinuxDuck
10-16-2002, 11:44 AM
Not to be rude, but a simple search through this forum will show that this type of question has been asked, and answered, many times. Just search for "unbuffered input" and you'll get a whole slew of results that will tell you exactly what you need to know.

gedger
10-16-2002, 12:10 PM
Thanks for that I have been searching all day.

I was putting in the wrong search criteria.

gedger
10-16-2002, 02:40 PM
Hi,

I still can't find the code I want it is really frustrating.

Why wasn't conio included with g++

PLEASE HELP

DODGE

TheLinuxDuck
10-16-2002, 03:16 PM
Because conio.h is a DOS header.

The problem is that input is buffered, which means that you can press keys until you're blue in the face (or the buffer fills), but until you hit enter, those keys stay in the buffer..

To have a "press any key", you simply have to turn off keyboard buffering.. there are several solutions... some are tricky, some are frowned upon, some work, but require extra library overhead.

I guess you just have to take your pick and go for it.

Tricky: Thie requires using termios.h, and disabling input buffering. It's not pretty, but it works. Here is an example:
http://www.codeexamples.org/index.cgi?newBody=c2h/hl.cgi?filename=userinput.c

Frowned upon: Shell out, using system, and make calls to stty to adjust the buffering. man stty for more information

library overhead: ncurses is a nice library if you're going to use it for it's potential but merely for unbuffering input is alot of overhead for such small thing.
http://www.linuxnewbie.org/forum/showthread.php?s=&threadid=63552&highlight=unbuffered+input

The example halfway down is in C++, but the calls to ncurses are the same. If you use this, don't forget to compile in the ncurses library.