Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

text input w/o showing the input (akin to password prompts)

Status
Not open for further replies.

practicesmagic

Programmer
Jan 10, 2001
2
0
0
US
I'm wondering how to get input from the keyboard without echoing the characters to screen ...

Help?

So you know, I'm fairly new at programming

Thanks,
Nikolai
 
Hi,
If you are using an old compiler one way is to use getch().

char inchar = 0;
char password[20]={0} ;


for(x = 0; x < 19 &amp;&amp; inchar != 13 ; ++x)
{
inchar = getch();
if(inchar != 13) // 13 is RETURN
password[x] = inchar;
}

I think Borland compilers will also work. If you have there free compiler read the note in conio.h about getch().
If you have M$ VC++6 then I don't think this will work.

Pappy
 
This is what I threw together using your suggestion. I'm using VC++ 5 I think ... I added code to deal with backspaces and output asterisks for characters...


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

void getpasswd()
{
char inchar = 0;
char password[20]={0} ;

// Necessary to make sure whatever cout outputs is actually
// printed to the screen before getch() acts
cout.flush();

for (int x = 0; x < 19 &amp;&amp; inchar != 13 ; ++x)
{
inchar = getch();
if(inchar != 13) // 13 is RETURN
{
password[x] = inchar;
if (inchar == 8)
{
cout << &quot;\b \b&quot;;
x--;
}
else
cout.put('*');
cout.flush();
}
}

cout << &quot;\nThe password is \&quot;&quot; << password << &quot;\&quot;&quot; << endl;
}

int main() {
cout << &quot;Enter in a password (less than 19 characters) : &quot;;

getpasswd();

cout << &quot;\nProgram complete\n&quot;;

return 0;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top