Replacement Of getch()

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 so that I can submit my solutions here. But I 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.

please refer to following code snippet :

char * str = NULL;

str = getpass("Enter password : ");
puts(str);

the function you need is getpass() which has following signature :
char * getpass(const char * prompt);
defined in unistd.h

hopefully this helps!

to prevent the echoing in a TTY, you’ll probably have to deal with terminal-related libraries, like ncurses (for example).

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

1 Like

come on… this function hasn’t been designed for that ! :slight_smile: it’s a really dirty trick. you have to press enter to continue, and more than one character can be in str. if he wants to detect the pressing of upper arrow (for instance), that won’t do the job AT ALL. ^^

I tried to do the getpass trick, but it didn’t quite work out. Check out my code that i wrote in Turbo C++. It is working. Instead of echoing the input it prints ‘*’. Just will you be kind enough to transform it in acceptable GCC code

#include<string.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
 clrscr();
 char a[100]={'\0'};
char b[100]={'v','i','t','a','m','i','n','c'};
 for(int i=0;i<1000;i++)
	{
	 char c=getch();
	 if(c=='\r')
		break;
	 cout<<"*";
	 a[i]=c;
	}
 int d=strcmpi(a,b);
 if(d==0)
	cout<<"Password Matched!!!";
 else
	cout<<"Password Incorrect!!!";
 getch();
}

I agree with UncleBens’s comment at StackOverflow:

I have actually always wondered and meant to ask why some people always start a program with clrscr() and end with getch() in the first place.

Maybe you would like to read this one too.

Personally I do not want some program clear my screen.

it seems that @sucho do not want to do that…

I guess this should work for you. Replace getch() with getchar() which is under stdio.h

#include<string.h>
#include
#include<stdlib.h>
#include<stdio.h>
using namespace std;

int main()
{

char a[100]={’\0’};

char b[100]={'v','i','t','a','m','i','n','c'};
int i;
for(i=0;i<1000;i++)
{
	char c=getchar();
	if(c=='\n')
		break;
	cout<<"*";
	a[i]=c;
}
int d=strcmpi(a,b);
if(d==0)
	cout<<"Password Matched!!!";
else
	cout<<"Password Incorrect!!!";
return 0;

}

2 Likes

i know there is something called as noecho() in <curses.h>. but i am not sure how it is used. maybe you can try a little experiment with it and let us know too!

getchar() echoes the input on the screen. Also the input will be something like this-

vitaminc //press enter

(and then 8’*’)

and after this it will again ask for an input. getch() and getche() has an unique characteristic due to which it automatically passes on to the next line of code after a character is entered. Will not work
@ashish1992

//