Hello I'm new at using c++ and new at this forum as well, I'm wondering if you could help me with my atm project. Could you please show how to make my program accept pin number but display ***** or any other character, instead of showing numbers when inputting them? thank you very much in advance! More power!

#include <iostream.h>
#include <conio.h>
#include <stdlib.h>

main()
{
int newpin1, newpin2;
clrscr();

cout <<"Change PIN";
cout <endl;
clrscr();
cout <<"Please Enter Your New PIN"<<endl;
cin >>newpin1;
clrscr();
cout <<"Please Re-enter Your New PIN"<<endl;
cin >> newpin2;
clrscr();
if (newpin1 == newpin2)
{
cout <<"Succesful! Your PIN has been changed"<<endl;
}
else
{
cout<<"Incorrect PIN"<<endl;
}

getch ();
return 0;
}

I don't think there is a standard way to do this (if I'm wrong I would like to know) so you will probably have to rely on the operating system. If you're using Windows/Linux the exact functions will vary.

Windows:
http://msdn.microsoft.com/en-us/library/078sfkak(v=vs.80).aspx

The problem is that since, unless set otherwise, the keyboard uses buffered input and it echoes the keystrokes. Your code is blocked till the OS gives you back control, so whatever you put in your code to add asterisks or whatever won't execute till the damage is already done (strokes are seen).

You need to make the keyboard input unbuffered, have it not echo, "hook" it, divert it to somewhere other than the default behavior of making it display on the screen, etc. I'm not guaranteeing that that can't be done using the standard libraries, but a quick search for "C++ keyboard unbuffered input" seems to suggest that it can't.

On the plus side, that search also brings out a lot of solutions, many of which seem not all that complex, but so far they're all OS-specific, involve stuff like ncurses or Microsoft functions, etc. So it's definitely doable, but it looks like very likely not using only the standard libraries.

I noticed that you are using the non-standard library conio.h in your code, its a pretty outdated library and it would probably be better to use a different library, but it does provide support for input that doesn't echo to the screen with the getch() function (which you actually use in your code).

If you want to use conio.h, then something like this should work

string getPassword()
{
	string s = "";
	char c;
	while ( (c = getch()) != '\n' )
	{
		cout << '*';
		s += c;
	}
	
	return s;
}

I'm using borland c++ 5.02

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.