Replacementr Of getch()

Jan 29, 2013 at 5:36pm
Well I've been using Turbo C++ for quite a while. Then after finding out its quite obsolete, i switched to GCC compiler with Code::Blocks IDE. I recently found out that conio.h is not a standard library. Unfortunately I use getch() function quite a lot for preventing echoing the input on the screen and other purposes, which is under conio.h. Can anyone tell me an alternate to getch() with similiar function which falls under a standard library.
Last edited on Jan 29, 2013 at 5:38pm
Jan 29, 2013 at 6:30pm
I know mingw does have a conio.h header.
Jan 30, 2013 at 1:35am
As far as I can see in this site's reference, there isn't any such function. I would suggest using curses instead, as they are cross-platform.
Jan 30, 2013 at 10:39am
Hi.
This is a getch simulation in linux. search for something similar in windows.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <stdio.h>
#include <termios.h>
#include <unistd.h>
int getch()
{
	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;
}
Jan 30, 2013 at 11:40am
Just continue to use getch() and do not worry about being non standard, all modern windows compilers support it. (MinGW and Visual Studio have conio.h header).
Jan 30, 2013 at 1:03pm
Well modoran, I use Code::Blocks IDE with GNU GCC compiler. I wrote the code in it with a few changes though. After building it there is an error saying-
error:'getch' was not declared in this scope.
Also is there strcmpi is not working. It says-
cannot convert 'std::string {aka std::basic_string<char>}' to 'const char*' for argument '1' to 'int strcmp(const char*, const char*)'|... Please Help.
Jan 30, 2013 at 1:18pm
well the getch() problem is solved but the strcmpi froblem remains!!
Jan 30, 2013 at 10:14pm
use c_str() function of class string:
1
2
string str = "Hi";
char* charPtr = str.c_str();
Jan 31, 2013 at 11:58am
Thanks, It worked!
Topic archived. No new replies allowed.