View Full Version : conio.h?
Luggy
July 20th, 2005, 11:42 AM
I've been taugh c/c++ under microsoft visual studio 6.0 and I've noticed that whenever I'm compiling code with g++ it cannot find the conio.h header file.
Is this library just a windows thing? Is the header file hiding somewhere? Is there an equivilant I should be using?
I miss getch() :(
tomchuk
July 20th, 2005, 01:46 PM
conio.h and getch() are windows specific. For a good explination read: http://www.ale.org/archive/ale/ale-2000-07/msg00442.html
vintem
July 22nd, 2005, 05:47 AM
Use <iostream> instead of <conio.h>.
Then you have cin to get input and cout to output.
For example this small menu:
int selection
cout << "1) new file" << endl; // endl jumps to the nextline
cout << "2) open file" << endl;
cout << "3) exit" <<endl;
cout << "Enter your choice (1,2 or 3): ";
cin >> selection;
The user is invited to type his choice (1,2,3) which will be stored in selection, which can be processed in a switch-statement.
The double arrows show the direction: with cin ">>" points to the variable in which the input should be stored.
With cout "<<" points to the standard output device (your screen).
cin can also handle datatypes other than int.
Use <iostream> instead of <iostream.h> because the latter isn't in ANSI C++.
Once you get used to cin, you won't miss getch().
Hanj
July 22nd, 2005, 04:28 PM
Once you get used to cin, you won't miss getch().getch returns the next character typed (without waiting for return), and afaik there is no way to do that with cin. Under linux, take a look at curses.h ('man ncurses'). It contains the functions you need.
glacierre
January 18th, 2008, 07:47 AM
Quite an old thread but just was searching something related. There are some (lower level) ways of getting single key presses without curses:
#include <termios.h>
#include <unistd.h>
int getkey( ) {
struct termios oldt,
newt;
int ch;
tcgetattr( STDIN_FILENO, &oldt );
newt = oldt;
newt.c_lflag &= ~( ICANON | ECHO );
tcsetattr( STDIN_FILENO, TCSANOW, &newt );
ch = getchar();
tcsetattr( STDIN_FILENO, TCSANOW, &oldt );
return ch;
}
vBulletin® v3.8.7, Copyright ©2000-2012, vBulletin Solutions, Inc.